Django NoReverseMatch

I have the following setup:

/landing_pages views.py urls.py 

In urls.py I have the following that works when I try to access /competition :

 from django.conf.urls.defaults import * from django.conf import settings from django.views.generic.simple import direct_to_template from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"), ) 

My views.py has something like this:

 def page(request, page_name): return HttpResponse('ok') 

Then in the template, I try to do this:

 {% load url from future %} <a href="{% url 'landing_pages.views.page' page_name='competition'%}"> Competition </a> 

What I apparently can't do:

Caught NoReverseMatch on rendering: Reverse for 'landing_pages.views.page' with arguments' () 'and keyword arguments' {' page_name ': u'competition'} 'not found.

What am I doing wrong?

+6
source share
2 answers

In your comment on DrTyrsa, you ask why you cannot use args or kwargs. Think about it for a moment. The {% url %} tag displays - as the name implies - the actual URL that the user can click on. But you did not specify a space in the URL pattern for the arguments. Where will they go? What does the url look like? How it works?

If you want to allow the user to specify arguments for your view, you need to provide a URL pattern with a space for these arguments.

+10
source
 {% url [project_name].landing_pages.views.page page_name='competition' %} 

Or better

 {% url competition_landing 'competition' %} 
0
source

Source: https://habr.com/ru/post/893852/


All Articles