本文介绍了django url NoReverseMatch在第三方应用程序中工作,但不是我的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将django-zinnia 0.12.3整合到我的django 1.4项目中。我已经安装和工作了。我想覆盖它的zinnia / base.html模板,以便所有的内容使用我的网站的模板出现。 zinnia模板扩展'zinnia / base.html'。



当我将文件zinnia / templates / zinnia / base.html复制到myproject / templates / zinnia / base。 html所有的zinnia {%url%}停止工作,并提供NoReverseMatch。即使我对文件做了零修改。例如:

  {%url'zinnia_entry_archive_index'%}  - >返回:
NoReverseMatch反向为''zinnia_entry_archive_index''...未找到
{%url'admin:app_list''zinnia'%}title ={%transDashboard%} - >返回:
NoReverseMatch u'admin不是注册的命名空间

我已经解决了通过删除url名称周围的引号,例如:

  {%url zinnia_entry_archive_index%} 

但是,如果我删除了我放在myproject / templates / zinnia中的base.html的副本(换句话说,它使用原始的我的问题是为什么它可以与原始zinnia文件夹中的引号配合使用,但不能从我的项目文件夹中进行操作。



解决方案

原因是在 Django< = 3 url标记使用不带引号的url名称。但是在 Django 1.4 + 中,它已被弃用,并且没有引号的url名称在 Django 1.5 中消失:



所以如果你使用 Django< = 1.4 不要删除url名称周围的引号(除非你传递上下文变量)。相反,如果您想要升级您的django版本,请执行此操作:

  {%从未来添加网址%} 
{%url'zinnia_entry_archive_index'%}


I am integrating django-zinnia 0.12.3 into my django 1.4 project. I have it installed and working. I want to override its zinnia/base.html template so that all the content appears using my site's template. The zinnia templates extend 'zinnia/base.html'.

When I copy the file zinnia/templates/zinnia/base.html to myproject/templates/zinnia/base.html all the zinnia {% url %} stopped working and gave NoReverseMatch. Even if I made zero changes to the file. For example:

{% url 'zinnia_entry_archive_index' %}  --> returns:
    NoReverseMatch Reverse for ''zinnia_entry_archive_index'' ... not found
{% url 'admin:app_list' 'zinnia' %}" title="{% trans "Dashboard" %} --> returns:
    NoReverseMatch u"'admin" is not a registered namespace

I was solved the problem by removing the quotes around the url names, for example:

{% url zinnia_entry_archive_index %}

However, if I remove the copy of base.html that I put in myproject/templates/zinnia (in other words it uses the original one in the zinnia project), the urls work again.

My question is why does it work with the quotes inside the original zinnia folder, but not from within my project folder?

解决方案

The reason is that in Django <= 3 the url tag takes url name without quotes. But in Django 1.4+ it is deprecated and url name without quotes is gone in Django 1.5:

So if you are using Django <= 1.4 do not remove the quotes (unless you are passing context variable) around url names. Instead do this for compatibility reason if you ever wanted to upgrade your django version:

{% load url from future %}
{% url 'zinnia_entry_archive_index' %}

Documentation

这篇关于django url NoReverseMatch在第三方应用程序中工作,但不是我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:05