Django NoReverseMatch

20,542

You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?

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

Share:
20,542
codeimplementer
Author by

codeimplementer

CTO at Media Pop, hire us for DevOps and product development

Updated on July 09, 2022

Comments

  • codeimplementer
    codeimplementer almost 2 years

    I have the following setup:

    /landing_pages
        views.py
    urls.py
    

    In urls.py I have the following which 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 a template I'm trying to do this:

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

    Which I apparently can't do:

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

    What am I doing wrong?

  • DrTyrsa
    DrTyrsa almost 13 years
    @Kit Sunde Even second variation? Try to cause Http404 somehow (/404) and see wheteher your urls are imported.
  • codeimplementer
    codeimplementer almost 13 years
    Yes both (or all 3 including my example) produced the same error. I can access the page normally by going to /competition so the view and URL must be imported. In fact if I comment out the line from my url.py it stops working so there's nothing overriding it and handling it elsehwere.
  • DrTyrsa
    DrTyrsa almost 13 years
    And just {% url competition_landing %}? I think, no need to pass page_name if you are already passing it in urls.
  • codeimplementer
    codeimplementer almost 13 years
    Ah! That works. I still don't understand why I can't use args or kwargs with the view though.
  • DrTyrsa
    DrTyrsa almost 13 years
    I assume that if you pass a parameter in url, this url can't take this parameter again. Seems something like functools.partial to me.
  • codeimplementer
    codeimplementer almost 13 years
    OHHHHHHHHHHH! I understand the issue now, thank you. That was very insightful for me.
  • Niklas R
    Niklas R over 11 years
    I don't get it.. Could someone please explain a little more? ^^
  • Daniel Roseman
    Daniel Roseman over 11 years
    @NiklasR Please post a new question rather than commenting on a 15-month-old answer.
  • Niklas R
    Niklas R over 11 years
    Yea, I'm sorry.. But I already fixed it. I was running Django 1.4.? and had to include {% load url from future %} before using the url-tag with quotes..