Django NoReverseMatch

时光总嘲笑我的痴心妄想 提交于 2019-12-21 04:52:50

问题


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?


回答1:


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.




回答2:


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

Or better

{% url competition_landing 'competition' %}


来源:https://stackoverflow.com/questions/6871226/django-noreversematch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!