在Django中使用{%url%}时的通用视图NoReverseMatch

时间:2021-07-11 19:32:33

I have a problem to do the reverse command 'url' from the template base.html.

我有问题从模板base.html执行反向命令'url'。

URLS.conf my file looks like this:

URLS.conf我的文件如下所示:

dic_info_artigo = {
                  'queryset': Artigo.modificado.all(),
                  'date_field': 'data_pub',
}

urlpatterns = patterns('django.views.generic.date_based',
    (r'^$', 'archive_index', dic_info_artigo,'artigos'),

    (r'^(?P<year>\d{4})/$','archive_year', dic_info_artigo,'artigos_ano'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
        'archive_month', dic_info_artigo,'artigos_mes'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
        'archive_day', dic_info_artigo,'artigos_dia'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
        'object_detail', dic_info_artigo,'detalhe_artigo'),
)

base.html

<a href="{% url artigos %}"> Artigos </ a>

The error:

dictionary update sequence element # 0 has length 1; 2 is required

字典更新序列元素#0的长度为1; 2是必需的

Already tried using the parameter 'name=', i change the value , but it did not work

已经尝试使用参数'name =',我改变了值,但它没有用

url(r'^$', 'archive_index', dic_info_artigo, name='artigos'),

What am I doing wrong? Any tips?

我究竟做错了什么?有小费吗?

Thanks.

2 个解决方案

#1


1  

The error message suggests that you have tried to name a view using something like:

错误消息表明您尝试使用以下内容命名视图:

(r'^my_url$', 'my_view', 'my_view')

However, the third argument should be a dictionary, not the name of a view.

但是,第三个参数应该是字典,而不是视图的名称。

To prevent errors like this, I recommend always using the url shortcut and naming the url pattern:

为了防止这样的错误,我建议始终使用url快捷方式并命名url模式:

url(r'^my_url$', 'my_view', name='my_view')

However, you could pass an empty dictionary as the third argument if prefer:

但是,如果愿意,您可以将空字典作为第三个参数传递:

(r'^my_url$', 'my_view', {}, 'my_view')

The urls.py you have posted looks ok, so the problem is probably in a different urls.py. If you're lucky, the full traceback might give you the exact line of the module where the error is occurring.

您发布的urls.py看起来没问题,所以问题可能在于另一个urls.py.如果幸运的话,完整的回溯可能会为您提供发生错误的模块的确切行。

#2


0  

Use url() to name the urls and try the following in template file.

使用url()命名URL并在模板文件中尝试以下操作。

{% url 'artigos' %}

{%url'artigos'%}

#1


1  

The error message suggests that you have tried to name a view using something like:

错误消息表明您尝试使用以下内容命名视图:

(r'^my_url$', 'my_view', 'my_view')

However, the third argument should be a dictionary, not the name of a view.

但是,第三个参数应该是字典,而不是视图的名称。

To prevent errors like this, I recommend always using the url shortcut and naming the url pattern:

为了防止这样的错误,我建议始终使用url快捷方式并命名url模式:

url(r'^my_url$', 'my_view', name='my_view')

However, you could pass an empty dictionary as the third argument if prefer:

但是,如果愿意,您可以将空字典作为第三个参数传递:

(r'^my_url$', 'my_view', {}, 'my_view')

The urls.py you have posted looks ok, so the problem is probably in a different urls.py. If you're lucky, the full traceback might give you the exact line of the module where the error is occurring.

您发布的urls.py看起来没问题,所以问题可能在于另一个urls.py.如果幸运的话,完整的回溯可能会为您提供发生错误的模块的确切行。

#2


0  

Use url() to name the urls and try the following in template file.

使用url()命名URL并在模板文件中尝试以下操作。

{% url 'artigos' %}

{%url'artigos'%}