简体   繁体   English

命名的URL在Django 1.5和Python 2.7中不起作用

[英]named url not working in django 1.5 and python 2.7

I'm developing my own blog. 我正在开发自己的博客。 Everything was good. 一切都很好。 I tried prepare it for deployment which was not successful. 我尝试为部署做准备,但没有成功。 Now I undo all changes, but named url not working now (they were worked perfectly before): error: 现在,我撤消所有更改,但是命名的url现在无法正常工作(它们之前工作得非常好):

Reverse for ''main_page'' with arguments '()' and keyword arguments '{}' not found 找不到参数“()”和关键字参数“ {}”的“ main_page”

url: 网址:

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

main page view: 主页面视图:

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

named url used in: 命名网址用于:

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

Replace {% url 'main_page' %} with {% url main_page %} . {% url 'main_page' %}替换为{% url main_page %}

Quote from django 1.5 changelog : 引用django 1.5 changelog

One deprecated feature worth noting is the shift to “new-style” url tag. 值得注意的一项不推荐使用的功能是转向“新型” URL标签。 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之前,诸如{%url myview%}之类的语法被错误地解释(Django认为“ myview”是视图的文字名称,而不是名为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. Django 1.3和更高版本引入了{%from future future}语法,以引入更正的行为,其中myview被视为变量。

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" %}. 这样做的结果是,如果您在模板中未使用{%将来的%}加载URL,则需要将{%url myview%}之类的标签更改为{%url“ myview”%}。 If you were using {% load url from future %} you can simply remove that line under Django 1.5 如果您使用的是{未来的%加载网址},则可以在Django 1.5下简单地删除该行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM