【问题标题】:Getting the admin site to work in Django Project让管理站点在 Django 项目中工作
【发布时间】:2012-06-09 02:06:55
【问题描述】:

我正在尝试让管理站点为我的 Django 项目工作。我正在关注本教程https://docs.djangoproject.com/en/1.4/intro/tutorial02/,并且我正在使用 Django 1.4。

网址.py:

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^$', include('polls.urls')),
    # Examples:
    # url(r'^$', 'Blog.views.home', name='home'),
    # url(r'^Blog/', include('Blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

settings.py 是:

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'dev.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

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

)

我得到的错误信息是:

DoesNotExist at /admin/
Site matching query does not exist.
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/
Django Version: 1.4
Exception Type: DoesNotExist
Exception Value:    
Site matching query does not exist.
Exception Location: /Users/IMAC/work3/env/lib/python2.7/site-packages/django/db/models/query.py in get, line 366
Python Executable:  /Users/IMAC/work3/env/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/IMAC/work3/Blog',
 '/Users/IMAC/work3/env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Users/IMAC/work3/env/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
 '/Users/IMAC/work3/env/lib/python27.zip',
 '/Users/IMAC/work3/env/lib/python2.7',
 '/Users/IMAC/work3/env/lib/python2.7/plat-darwin',
 '/Users/IMAC/work3/env/lib/python2.7/plat-mac',
 '/Users/IMAC/work3/env/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/IMAC/work3/env/Extras/lib/python',
 '/Users/IMAC/work3/env/lib/python2.7/lib-tk',
 '/Users/IMAC/work3/env/lib/python2.7/lib-old',
 '/Users/IMAC/work3/env/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/IMAC/work3/env/lib/python2.7/site-packages']

不确定是什么问题?需要一些指导。将不胜感激。

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    settings.py:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'data.db',                      # Or path to database file if using sqlite3.
            'USER': '',                      # Not used with sqlite3.
            'PASSWORD': '',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
    }
    
    
    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # Uncomment the next line to enable the admin:
        'django.contrib.admin',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
        'try',
    )
    

    urls.py:

    from django.conf.urls import patterns, include, url
    
    
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
    
        url(r'^admin/', include(admin.site.urls)),
    )
    

    运行 syncdb 以创建数据库和用户。那么它应该可以工作了。

    【讨论】:

    • 首先,感谢您的帮助。我仔细检查了错误,我还发布了上面的 urls.py.. 看看...
    • 你需要运行 python manage.py syncdb。所以你可以创建数据库并创建一个超级用户。我要改变我的答案给你看。
    • 如果您在添加管理员之前运行它,您可能需要删除数据库并重新运行 syncdb。另外,您使用的是编译器吗?
    • 你需要一个超级用户,你在运行syncdb时创建一个,它是你用来登录管理页面的用户名和密码。
    • 当我删除数据库并重新创建它时,它会问我:您刚刚安装了 Django 的身份验证系统,这意味着您没有定义任何超级用户。您想现在创建一个吗? (是/否):当我输入是时,它不允许创建一个......为什么会这样?
    【解决方案2】:

    我在教程中遇到了同样的问题。您在尝试创建超级用户时提到了 Python 错误,是下面的错误吗?

    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 428, in _parse_localename
        raise ValueError, 'unknown locale: %s' % localename
    ValueError: unknown locale: UTF-8
    

    这与您终端的区域设置有关。有关 SO 解决方案,请参阅 here。 运行那里提到的两个导出命令,删除您的数据库文件,运行“python manage.py syncdb”再次创建数据库,输入“yes”创建超级用户。这不应该再给你一个错误,你现在应该可以访问本地主机上的管理站点了。

    【讨论】:

    • 先删除数据库然后再次运行syncdb对我也有用。
    【解决方案3】:

    迟到的回复..

    但是我从这里得到了解决这个问题的方法

    http://satishgandham.com/2012/04/error-whil-creating-super-user-in-django1-4-on-mac/

    根据评论编辑


    在添加用户之前在终端中运行此命令已解决此问题

    export LANG="en_US.UTF-8"
    

    我猜这个字符编码问题是在运行 syncdb 时访问 django1.4 的管理部分时出现以下错误的原因

    【讨论】:

    • 这基本上是一个仅链接的答案,这不是我们希望在 Stack Overflow 上看到的。请解释回答发帖者问题的链接页面的内容,包括一些演示修复的代码。
    猜你喜欢
    • 2017-12-26
    • 2017-12-12
    • 2015-07-07
    • 1970-01-01
    • 2011-09-23
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多