Arjun Kashyap
Arjun Kashyap

Reputation: 663

Why m I getting this error in django urls?

enter image description hereI am very new to Django and still trying to understand most things. l am currently working on a project where I need to add a details page for each link. But I am getting the following error. Error:

NoReverseMatch at /discover/

Reverse for 'disc-details' with arguments '('',)' not found. 1 pattern(s) tried: ['discover/(?P<discover_id>[0-9]+)/']

Following is my urls patterns for app:

url(r'discover/$', views.discover, name = 'discover'),
url(r'discover/(?P<discover_id>[0-9]+)/', views.disc_details, name = 'disc-details'),

and my views.py look something like:

def discover(request):
    disc = Discover.objects.all()
    return render(request, 'main/discover.html', {'disc': disc})

def disc_details(request, discover_id):
    dis = get_object_or_404(Discover, pk = discover_id)
    det = Discover.objects.get(pk = discover_id)
    return render(request, 'main/discDetails.html', {'det': det, 'dis': dis})

Whats happening here is discover is a html page containing various links and each link should have its on disc_details section. My html section has the following:

<a class="btn btn-success" href='{% url 'main:disc-details' dis.id%}' class = "detail-link">Details</a>

Help me fix this and please provide an explanation.

Upvotes: 0

Views: 34

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47374

Your context variable name is disc:

def discover(request):
    disc = Discover.objects.all()
    return render(request, 'main/discover.html', {'disc': disc})

So you should use disc in discover.html template also. Note you should iterate over disc values to get disc objects:

{% for el in disc %}
    '{% url 'main:disc-details' el.id %}'
{% endfor %}

Upvotes: 1

Related Questions