Django 命名的 url 无法被模板 url 标签识别
在projects/urls.py 中我有:
urlpatterns = patterns('bizteen.projects.views',
url(r'^browse/$', 'browse', name='projects-browse-main'),
url(r'^browse/(\d+)/$', 'browse', name='projects-browse'),
url(r'^create/$', 'create', name='projects-create'),
url(r'^(\d+)/$', 'view_project', name='projects-view'),
)
在模板中我有:
<a href="{% url projects-browse-main %}">Browse projects</a>
但出现错误。
TemplateSyntaxError at /
Caught an exception while rendering: Reverse for 'bizteen.projects-browse-main' with arguments '()' and keyword arguments '{}' not found.
我最多能弄清楚的是,名称被视为视图。 为什么会出现这种情况呢?
来自 bizteen.projects.views:
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from bizteen.projects.models import Project, ProjectComment
from django.template import RequestContext
def browse(request, page=0):
page_start = page * 25
page_end = page_start + 25
project_list = Project.objects.order_by('date_created').reverse([page_start:page_end]
return render_to_response('browse_projects.html', {'project_list': project_list}, context_instance=RequestContext(request))
和主要 urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^users/', include('bizteen.userthings.urls')),
(r'^projects/', include('bizteen.projects.urls')),
url(r'^$', 'bizteen.userthings.views.home', name='main-home'),
url(r'^profile/$', 'bizteen.userthings.user-profile'),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/(.*)', admin.site.root),
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'templates/static'}),
)
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.0.2 final
Python Version: 2.5.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'bizteen.userthings',
'bizteen.projects']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Template error:
In template /Users/xenon/bizteen/trunk/web/bizteen/templates/base.html, error at line 33
Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
23 : <div class="span-24">
24 : <div class="span-10"> </div>
25 : <div class="span-8">
26 : <div id='quick_user_menu'>
27 : <b>Hi there!</b>
28 : {% if user.is_authenticated %}
29 : <b><a href="{{ user.get_absolute_url }}">{{ user.username }}</a></b>
30 : <a href='/user/home/' class='lightblue'>Dashboard</a> |
31 : (<a href='/user/logout/' class='anchor'>logout</a>)
32 : {% else %}
33 : <a href=' {% url main-home %} ' class='lightblue' rel='facebox'>Sign In</a>
34 : (<a href='/user/signup/' class='anchor'>Don't have an account?</a>)
35 :
36 : {% endif %}
37 : </div>
38 : </div>
39 : </div>
40 : <div class="span-24">
41 : <div id='header'>
42 : <div class="span-8">
43 : <A href='/user/home/'><img src='http://localhost:8000/site_media/images/logo.png' alt='BizTeen logo image' align='left'/></a>
Traceback:
File "/Library/Python/2.5/site-packages/django/core/handlers/base.py" in get_response
86. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/xenon/bizteen/trunk/web/bizteen/../bizteen/userthings/views.py" in home
29. return render_to_response('home.html', context_instance=RequestContext(request))
File "/Library/Python/2.5/site-packages/django/shortcuts/__init__.py" in render_to_response
18. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Python/2.5/site-packages/django/template/loader.py" in render_to_string
107. return t.render(context_instance)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
176. return self.nodelist.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
71. result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/loader_tags.py" in render
97. return compiled_parent.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
176. return self.nodelist.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
71. result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/defaulttags.py" in render
246. return self.nodelist_false.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
81. raise wrapped
Exception Type: TemplateSyntaxError at /
Exception Value: Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
Original Traceback (most recent call last):
File "/Library/Python/2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/defaulttags.py", line 378, in render
args=args, kwargs=kwargs)
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 254, in reverse
*args, **kwargs)))
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 243, in reverse
"arguments '%s' not found." % (lookup_view, args, kwargs))
NoReverseMatch: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
In projects/urls.py I have:
urlpatterns = patterns('bizteen.projects.views',
url(r'^browse/
And in a template I have:
<a href="{% url projects-browse-main %}">Browse projects</a>
But an error shows up.
TemplateSyntaxError at /
Caught an exception while rendering: Reverse for 'bizteen.projects-browse-main' with arguments '()' and keyword arguments '{}' not found.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from bizteen.projects.models import Project, ProjectComment
from django.template import RequestContext
def browse(request, page=0):
page_start = page * 25
page_end = page_start + 25
project_list = Project.objects.order_by('date_created').reverse([page_start:page_end]
return render_to_response('browse_projects.html', {'project_list': project_list}, context_instance=RequestContext(request))
and the main urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^users/', include('bizteen.userthings.urls')),
(r'^projects/', include('bizteen.projects.urls')),
url(r'^
traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.0.2 final
Python Version: 2.5.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'bizteen.userthings',
'bizteen.projects']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Template error:
In template /Users/xenon/bizteen/trunk/web/bizteen/templates/base.html, error at line 33
Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
23 : <div class="span-24">
24 : <div class="span-10"> </div>
25 : <div class="span-8">
26 : <div id='quick_user_menu'>
27 : <b>Hi there!</b>
28 : {% if user.is_authenticated %}
29 : <b><a href="{{ user.get_absolute_url }}">{{ user.username }}</a></b>
30 : <a href='/user/home/' class='lightblue'>Dashboard</a> |
31 : (<a href='/user/logout/' class='anchor'>logout</a>)
32 : {% else %}
33 : <a href=' {% url main-home %} ' class='lightblue' rel='facebox'>Sign In</a>
34 : (<a href='/user/signup/' class='anchor'>Don't have an account?</a>)
35 :
36 : {% endif %}
37 : </div>
38 : </div>
39 : </div>
40 : <div class="span-24">
41 : <div id='header'>
42 : <div class="span-8">
43 : <A href='/user/home/'><img src='http://localhost:8000/site_media/images/logo.png' alt='BizTeen logo image' align='left'/></a>
Traceback:
File "/Library/Python/2.5/site-packages/django/core/handlers/base.py" in get_response
86. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/xenon/bizteen/trunk/web/bizteen/../bizteen/userthings/views.py" in home
29. return render_to_response('home.html', context_instance=RequestContext(request))
File "/Library/Python/2.5/site-packages/django/shortcuts/__init__.py" in render_to_response
18. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Python/2.5/site-packages/django/template/loader.py" in render_to_string
107. return t.render(context_instance)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
176. return self.nodelist.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
71. result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/loader_tags.py" in render
97. return compiled_parent.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
176. return self.nodelist.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
71. result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/defaulttags.py" in render
246. return self.nodelist_false.render(context)
File "/Library/Python/2.5/site-packages/django/template/__init__.py" in render
768. bits.append(self.render_node(node, context))
File "/Library/Python/2.5/site-packages/django/template/debug.py" in render_node
81. raise wrapped
Exception Type: TemplateSyntaxError at /
Exception Value: Caught an exception while rendering: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
Original Traceback (most recent call last):
File "/Library/Python/2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/Library/Python/2.5/site-packages/django/template/defaulttags.py", line 378, in render
args=args, kwargs=kwargs)
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 254, in reverse
*args, **kwargs)))
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 243, in reverse
"arguments '%s' not found." % (lookup_view, args, kwargs))
NoReverseMatch: Reverse for 'bizteen.main-home' with arguments '()' and keyword arguments '{}' not found.
, 'browse', name='projects-browse-main'),
url(r'^browse/(\d+)/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'browse', name='projects-browse'),
url(r'^create/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'create', name='projects-create'),
url(r'^(\d+)/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'view_project', name='projects-view'),
)
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'bizteen.userthings.views.home', name='main-home'),
url(r'^profile/
traceback:
, 'browse', name='projects-browse-main'),
url(r'^browse/(\d+)/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'browse', name='projects-browse'),
url(r'^create/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'create', name='projects-create'),
url(r'^(\d+)/
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'view_project', name='projects-view'),
)
And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'bizteen.userthings.user-profile'), url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/(.*)', admin.site.root), url(r'^site_media/(?P<path>.*)traceback:
, 'browse', name='projects-browse-main'), url(r'^browse/(\d+)/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'browse', name='projects-browse'), url(r'^create/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'create', name='projects-create'), url(r'^(\d+)/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'view_project', name='projects-view'), )And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'django.views.static.serve', {'document_root': 'templates/static'}), )traceback:
, 'browse', name='projects-browse-main'), url(r'^browse/(\d+)/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'browse', name='projects-browse'), url(r'^create/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'create', name='projects-create'), url(r'^(\d+)/And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
, 'view_project', name='projects-view'), )And in a template I have:
But an error shows up.
The most I can figure out is that the names are being treated like views instead. Why would this happen?
from bizteen.projects.views:
and the main urls.py:
traceback:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这可能是因为
两者都引用了“bizteen.projects.views”中的相同函数“浏览”,但其中一个带有参数(\d+),而另一个则没有。 您是否为该函数指定了默认参数? 如果不是这样
,您的项目范围的 urls.py 中可能存在问题(您的示例看起来像是应用程序 urls.py 的摘录)。 你的项目范围的 urls.py 中有类似的东西吗?
I think it might be because
both refer to the same function 'browse' in 'bizteen.projects.views' but one of them takes an argument (\d+) and the other one doesn't. Have you specified a default parameter for the function? Like so
If not that, there might be a problem in your project-wide urls.py (your example looks like an excerpt from an application urls.py). Do you have something like this in your project-wide urls.py?
从我的 urls.py 来看:
该视图不存在。 我觉得自己很蠢。
From my urls.py:
That view didn't exist. I feel dumb.
我个人在 urls.py 中导入我的视图,并将它们作为 url() 函数的第二个参数:
它避免出现运行时错误,并允许您添加像 'login_required' 这样的装饰器:
I personnaly import my view in my urls.py and give them as second argument of url() fonctions:
It avoids getting runtime errors, and allows you to add decorators like 'login_required' :