Django admin - 'NoneType'对象没有属性'rindex'

时间:2011-06-09 15:46:05

标签: python django django-admin

我正在阅读Django 1.0网站开发书。在第8章,我无法使管理应用程序工作。这是我得到的追溯。

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 1.3
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.comments',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_bookmarks.bookmarks',
 'debug_toolbar',
 'django.contrib.admin',
 'django.contrib.admindocs']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/usr/local/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/local/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
  158.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/usr/local/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_callback
  169.             mod_name, func_name = get_mod_func(self._callback_str)
File "/usr/local/lib/python2.6/site-packages/django/core/urlresolvers.py" in get_mod_func
  113.         dot = callback.rindex('.')

Exception Type: AttributeError at /admin/
Exception Value: 'NoneType' object has no attribute 'rindex'

这是来自settings.py文件的我的INSTALLED_APPS部分:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.comments',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_bookmarks.bookmarks',
    'debug_toolbar',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
)

这是我的admin.py文件:

from django.contrib import admin
from bookmarks.models import *


class LinkAdmin(admin.ModelAdmin):
  pass


class TagAdmin(admin.ModelAdmin):
  pass


class BookmarkAdmin(admin.ModelAdmin):
  list_display = ('title', 'link', 'user')
  list_filter = ('user',)
  ordering = ('title',)
  search_fields = ('title',)


admin.site.register(Link, LinkAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Bookmark, BookmarkAdmin)

这是我的urls.py文件:

import os
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from bookmarks.views import *
from django.contrib import admin
admin.autodiscover()


site_media = os.path.join(os.path.dirname(__file__), 'site_media')


urlpatterns = patterns('',
    # Admin interface
    (r'^admin/(.*)', admin.site.root_path),

    # Browsing
    (r'^$', main_page),
    (r'^popular/$', popular_page),
    (r'^user/(\w+)/$', user_page),
    (r'^tag/([^\s]+)/$', tag_page),
    (r'^tag/$', tag_cloud_page),
    (r'^search/$', search_page),
    (r'^bookmark/(\d+)/$', bookmark_page),

    # Session management
    (r'^login/$', 'django.contrib.auth.views.login'),
    (r'^logout/$', logout_page),
    (r'^register/$', register_page),
    (r'^register/success/$', direct_to_template,
    {'template': 'registration/register_success.html'}),

    # Account management
    (r'^save/$', bookmark_save_page),
    (r'^vote/$', bookmark_vote_page),

    # Site media
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': site_media}),

    # Ajax
    (r'^ajax/tag/autocomplete/$', ajax_tag_autocomplete),

    # Comments
    (r'^comments/', include('django.contrib.comments.urls')),

)

我已经尝试从本书的源代码= NADA中复制urls.py文件。我已经阅读了关于属性错误的各种其他论坛帖子,但似乎找不到与我匹配的场景。你的想法&amp;建议表示赞赏。

3 个答案:

答案 0 :(得分:3)

这是:

(r'^admin/(.*)', admin.site.root_path),

导致错误,因为admin.site.root_path等于None

正确的值是:

(r'^admin/(.*)', include(admin.site.urls)),

答案 1 :(得分:0)

期望callback变量是一个字符串,但它正在变为None。我要检查的第一件事是你在正确的位置debug_toolbar并正确引用。移除debug_toolbar并查看错误是否消失。

答案 2 :(得分:0)

检查此问题:

reverse() throws AttributeError on call

也许你在urls元组中作为第二项传递的一些函数的计算结果为None。