Midi_
Midi_

Reputation: 219

Django cant pass id with url

I have read a number of other posts and the django docs and as far as i can tell i am doing this correctly but for some reason it is not working. I want to pass an items id through the url the correct way.

This works

<a href="/item_details/{{recent.id}}">

But this doesn't

<a href="{% url 'my_app:item_details' recent.id %}">

or this

<a href="{% url 'item_details' recent.id %}">

urls

url(r'^item_details/(?P<item_id>\d+)/$', views.item_details, name='view_item_details'),

full code

{% for recent, images in recent_and_images %}
        <div class="item-wrapper">
            <div class="item-image-wrapper">
              <a href="{% url 'item_details' recent.id %}">
                <img src="{{images.0}}" width="300" alt="">
              </a>
            </div>
        </div>
{% endfor %}

error

Reverse for 'item_details' with arguments '(99,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Also my project is on Django 1.9. I checked the docs and they seem to implement this the same way however.

Upvotes: 0

Views: 530

Answers (2)

user10356004
user10356004

Reputation:

In Django 2 it will be:

{% url 'item_details' item_id=recent.id %}

Upvotes: 0

Vineeth Sai
Vineeth Sai

Reputation: 3455

Reverse searches for name attribute. Not the name of the function. So in your case you named your url view_item_details and you are passing item_details, which is incorrect. Django passes this value to reverse function and fetches the full URL automatically, And when it can't find a matching URL you get the reverse error. So replace your line in your template with this and it should work

<a href="{% url 'view_item_details' recent.id %}">

Upvotes: 1

Related Questions