python - 在 Django 中使用 {% url %} 时通用 View NoReverseMatch

标签 python django django-templates django-urls django-generic-views

我在从模板 base.html 执行反向命令“url”时遇到问题。

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>

错误:

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

已经尝试使用参数“name=”,我更改了值,但它不起作用

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

我做错了什么?有什么建议吗?

谢谢。

最佳答案

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

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

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

为了防止出现此类错误,我建议始终使用 url 快捷方式并命名 url 模式:

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

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

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

您发布的 urls.py 看起来没问题,因此问题可能出在不同的 urls.py 中。如果幸运的话,完整的回溯可能会为您提供发生错误的模块的确切行。

关于python - 在 Django 中使用 {% url %} 时通用 View NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13036436/

相关文章:

python reduce accum 作为参数

python - Geojson 到 MongoDB/Mongoengine 的属性格式

javascript - 通过 onload() 和刷新按钮刷新网页

Django - 如何根据使用权限动态生成菜单

python - 从元组中获取备用值

javascript - 使用 HTML5 &lt;input&gt; 字段抓取动态生成的网页

django - 使用 azuread-oauth2 时,exchange_access_token 上的 openEdx 服务器错误

python - 在django中过滤带注释的列

python - 用于动态显示字典的标签串联

django - 如何迭代模板中 SelectField 的选项?