命名url在Django 1.5和python 2.7中不起作用

2024-04-24 16:56:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发自己的博客。一切都很好。我试着准备部署,但没有成功。现在我撤消所有更改,但命名的url现在不起作用(它们以前工作得很好): 错误:

Reverse for ''main_page'' with arguments '()' and keyword arguments '{}' not found

网址:

urlpatterns = patterns('',
  url(r'^$', main_page, name='main_page'),
  url(r'^blog/', include('blog.blogurls')),
  url(r'^comments/', include('django.contrib.comments.urls')),
)

主页视图:

def main_page(request):
  object_list = Article.objects.all()
  return render_to_response('blog/main_page.html', {'Latest': object_list}

命名url用于:

<p><a href="{% url 'main_page' %}">home</a></p>

Tags: urlforobjectincludemain部署错误with
1条回答
网友
1楼 · 发布于 2024-04-24 16:56:56

{% url main_page %}替换{% url 'main_page' %}。你知道吗

引用django 1.5changelog

One deprecated feature worth noting is the shift to “new-style” url tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly (Django considered "myview" to be a literal name of a view, not a template variable named myview). Django 1.3 and above introduced the {% load url from future %} syntax to bring in the corrected behavior where myview was seen as a variable.

The upshot of this is that if you are not using {% load url from future %} in your templates, you’ll need to change tags like {% url myview %} to {% url "myview" %}. If you were using {% load url from future %} you can simply remove that line under Django 1.5

相关问题 更多 >