Horai Nuri
Horai Nuri

Reputation: 5568

why do my django urls render the wrong template?

I'm suprised that I cannot access my product detail page through the url and I don't understand why since I've already done this basic thing plenty of times...

I have a page where all my products are displayed, when the user click on a product he is redirected to the product detail, that's it.

Somehow when I click a link linked to the product detail or type de correct path to the url it loads the same page where all the product are shown but it doesn't even call the product detail view, why so ?

Here are the views :

def rcdex(request):
    list = Liste.objects.all()
    return render(request, 'rcdex.html', {'list':list,})

def rc_detail(request, id):
    list = Liste.objects.get(id=id)
    return render(request, 'rc_detail.html', {'list':list,})

Here are the urls :

url(r'^', views.rcdex, name="rcdex"),
url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),

Here is how I call the rc_detail view on the template :

<th><a href="{% url 'rc_detail' l.id %}">{{ l.entreprise }}</a></th>

I don't get why it doesn't show me the correct template (rc_detail.html) but instead reload rcdex.html ?

Upvotes: 0

Views: 1362

Answers (2)

vinay kumar
vinay kumar

Reputation: 593

you can also do like this..

url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),
url(r'^', views.rcdex, name="rcdex"),

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599450

You haven't terminated your rcdex urlpattern, so it matches everything. You should use a $:

url(r'^$', views.rcdex, name="rcdex"),

Upvotes: 7

Related Questions