开发者

reverse() throws AttributeError on call

开发者 https://www.devze.com 2023-03-11 20:11 出处:网络
I\'m trying to reverse a named url called blog-home but no matter what I try it always throws an AttributeError with the description \'NoneType\' object has no attribute \'rindex\'.

I'm trying to reverse a named url called blog-home but no matter what I try it always throws an AttributeError with the description 'NoneType' object has no attribute 'rindex'.

I've tried reverse("blog-home"), reverse("blogengine:blog-home") and even reverse("admin:index") and reverse(resolve("/admin/")) just to test if my code was the problem.

The traceback:

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'

I'm really not sure about why this happens, especially as I've used reverse successfully on other projects with the same version of Python and Django and I haven't found anything about this error anywhere else so far.

My urls.py:

from django.conf.urls.defaults import patterns, include, url

from . import views
from .models import Post, Tag

from django.views.generic import DetailView, ListView

urlpatterns = patterns('',
    url(r'^$', ListView.as_view(queryset=Post.objects.order_by('-pub_date')), name="blog-home"),
    url(r'^yadda/$', None)
)

Which is imported by this urls.py:

from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin
admin.autodiscover()

from django.contrib.sitemaps import FlatPageSitemap

class FlatPageSitemap(FlatPageSitemap):
    changefreq = "daily"

sitemaps = {
    'flatpages': FlatPageSitemap,
}

import django.contrib.sitemap开发者_如何学JAVAs.views
from . import blogengine

urlpatterns = 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)),
)

I was originally intending to use it from the Django templating system with the url tag ({% url blog-home %}).


Don't do that:

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

If you specify an URL in the conf, it must be bound to something. If you don't want to bind it, don't specify it.

0

精彩评论

暂无评论...
验证码 换一张
取 消