0

Django tries the wrong url pattern but I can not find my mistake.

urls.py

     url(r'^message/(?P<advertisementid>(\d+))/$', chat_view, name="chat_view_with_toid"),
     url(r'^profile/(?P<userid>(\d+))/$', profile_view, name="profile"),
     url(r'^details/(?P<advertisementid>(\d+))/$', details_view, name='details'),

views.py

def details_view(request, advertisementid):
    advertisement_data = Advertisement.objects.get(id=advertisementid)
    return render(request, "details.html", {"advertisement_data": advertisement_data})

def profile_view(request, userid):
    advertisements = Advertisement.objects.filter(user__id=userid)
    return render(request, "profile.html", { 'advertisements': advertisements } )

details.html (from where I want to resolve to the users profile)

<a href="{% url 'profile' userid=advertisement_data.user.id %}" style="text-decoration:none;">
<input type="submit" class="details-btn-profile" value="Profile"></a>

    <a href="{% url 'chat_view_with_toid' advertisementid=advertisement_data.id %}" 
    style="text-decoration:none">
    <input type="submit" class="details-btn-contact" value="Kontakt"></a>

main.html

<a href="{% url 'details' advertisementid=advertisement.id %}">
<img src="{% static advertisement.image.url  %}" alt="Image"/></a>

When I click on button class="details-btn-profile" Django gives me this error:

Reverse for 'details' with no arguments not found. 1 pattern(s) tried: ['details/(?P(\d+))/$']

Why does it resolve to details ? Any ideas ?

3
  • Is there a form around the given HTML code? Commented Oct 8, 2017 at 10:27
  • No but <a href should be enough ? Commented Oct 8, 2017 at 10:31
  • A form would control the behavior of any submit button inside. I'm not sure what happens if there is no form. There could be an implicit default one resulting into a GET to the same URL and the given error. Commented Oct 8, 2017 at 10:36

3 Answers 3

1

when you click the button you send form where action wrong may be empty attr action something like it:

<form action="">

you need set it:

<form action="{% url 'profile' userid=advertisement_data.user.id %}">
           <!-- ^^^^^^^^^^^^^-->

or if you want to go link by click the just change type

 <input type="button" class="details-btn-profile" value="Profile">
    <!-- ^^^^^^^^^^-->
Sign up to request clarification or add additional context in comments.

2 Comments

You are right but I really need a form for that ? I did it in another place just with <a href= etc. .. It should be enough or am I wrong ?
You are right using button is better than submit, thanks
0

The url name chat_view_with_toid doesn't seem to exist in your urls.py.

3 Comments

Yeah... although the actual error message suggests there's something else afoot... Doesn't look like the OP has provided it...
@AaronVisang so where are you using {% url 'details' ... %} - we can only see {% url 'chat_view_with_toid' ... %} in your template...
I add it now it was in my main.html
0

Sorry for wasting your time, I found my mistake. On profile.html I had this HTML-Code:

<a href="{% url 'details' %}" class="announce-hedline"><h3>BMW 320i Limousine</h3></a>

So "{% url 'details' %}" can not be resolved because of missing the advertisementid which it needs to be resolved.

I searched for mistakes in the wrong place.

Thank you all for your help !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.