0

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.

1
  • 1
    The error is in the discover.html, it looks like the the dis variable does not exists there. Commented Sep 27, 2018 at 9:46

1 Answer 1

1

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 %}
Sign up to request clarification or add additional context in comments.

3 Comments

It worked. Now can you explain this in simple language. (why are we looping ?)
@ArjunKashyap we need to loop over disc since it contains multiple disc objects actually. Django's queryset Discover.objects.all() will give you list of discs. So you need to use loop to get access for each disc separately.
So I tried the loop method which worked when I added one entry in the database. Yet when I add more than one (say 2) it is showing two details (in a single section where there is supposed to be only one) link and when I click on them it takes me to the first database entry object. (Hope I made myself clear). I am adding a picture

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.