gpt4 book ai didi

python - reverse() 在调用时抛出 AttributeError

转载 作者:行者123 更新时间:2023-11-28 23:05:18 25 4
gpt4 key购买 nike

我正在尝试反转一个名为 blog-home 的命名 url,但无论我尝试什么,它总是抛出一个 AttributeError 和描述 'NoneType'对象没有属性“rindex”

我试过 reverse("blog-home")reverse("blogengine:blog-home") 甚至 reverse("admin: index")reverse(resolve("/admin/")) 只是为了测试我的代码是否有问题。

回溯:

Traceback (most recent call last):  File "", line 1, in   File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 368, in reverse    app_list = resolver.app_dict[ns]  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 241, in _get_app_dict    self._populate()  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 208, in _populate    for name in pattern.reverse_dict:  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 229, in _get_reverse_dict    self._populate()  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 220, in _populate    lookups.appendlist(pattern.callback, (bits, p_pattern))  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 169, in _get_callback    mod_name, func_name = get_mod_func(self._callback_str)  File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\urlresolvers.py", line 113, in get_mod_func    dot = callback.rindex('.')AttributeError: 'NoneType' object has no attribute 'rindex'

我真的不确定为什么会发生这种情况,特别是因为我已经在具有相同版本的 Python 和 Django 的其他项目上成功地使用了 reverse 并且我没有发现任何关于此错误的信息到目前为止的任何其他地方。

我的 urls.py:

from django.conf.urls.defaults import patterns, include, urlfrom . import viewsfrom .models import Post, Tagfrom django.views.generic import DetailView, ListViewurlpatterns = patterns('',    url(r'^$', ListView.as_view(queryset=Post.objects.order_by('-pub_date')), name="blog-home"),    url(r'^yadda/$', None))

由这个 urls.py 导入:

from django.conf.urls.defaults import patterns, include, urlfrom django.contrib import adminadmin.autodiscover()from django.contrib.sitemaps import FlatPageSitemapclass FlatPageSitemap(FlatPageSitemap):    changefreq = "daily"sitemaps = {    'flatpages': FlatPageSitemap,}import django.contrib.sitemaps.viewsfrom . import blogengineurlpatterns = patterns('',    url(r'^', include(blogengine.urls)),    url(r'^sitemap\.xml', django.contrib.sitemaps.views.sitemap, {'sitemaps': sitemaps}),    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),    url(r'^admin/', include(admin.site.urls)),)

我原本打算使用带有 url 标签的 Django 模板系统 ({% url blog-home %})。

最佳答案

不要那样做:

url(r'^yadda/$', None)

如果您在 conf 中指定一个 URL,它必须绑定(bind)到某个东西。如果您不想绑定(bind)它,请不要指定它。

关于python - reverse() 在调用时抛出 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6268512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com