【问题标题】:Django admin page not showingDjango 管理页面未显示
【发布时间】:2011-05-09 12:42:03
【问题描述】:

我一直在关注民意调查教程,直到我应该有一个用于管理后端的登录页面。 http://docs.djangoproject.com/en/dev/intro/tutorial02/

相反,我得到这样的欢迎页面:

我在 INSTALLED_APPS 中启用了管理应用程序,同步了数据库并调整了 urls.py,所以我不确定问题出在哪里。

使用 mod_wsgi 运行 apache2。

urls.py: 从 django.conf.urls.defaults 导入 *

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

urlpatterns = patterns('',
    # Example:
    # (r'^testproject/', include('testproject.foo.urls')),

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

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

Settings.py:

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

表格:

数据库已更改

mysql> SHOW TABLES;
+----------------------------+
| Tables_in_django_test      |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_message               |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_session             |
| django_site                |
| polls_choice               |
| polls_poll                 |
+----------------------------+

【问题讨论】:

  • 切换到 django 网络服务器,因为 apache 给了我一些问题。管理页面 404 和 It works 页面不再是蓝色的。

标签: python django mod-wsgi


【解决方案1】:

这两行真的缩进了一个空格吗,如您的帖子中所示?

 from django.contrib import admin
 admin.autodiscover()

如果你这样做,你会得到一个 IndentationError。将它们与左边距齐平。


稍后:哦,我在上面的评论中看到您发现了这个缩进错误。将我的答案标记为社区 wiki。

【讨论】:

  • 很好看。顺便说一句,关于缩进的普遍共识是,我是制表符还是使用空格?标签似乎更容易。
  • @Keyo:普遍的共识是使用四个空格。我也更喜欢标签:你可以让你的编辑器显示一个标签,无论你想要多少空格。人们说,“但是如果你混合前导制表符和空格怎么办!?!?!??!没有制表符!!!”无论如何,如果我管理世界,我们都会使用标签。 YMMV。
  • 我也在关注教程,但显然是从“第 2 部分”开始,没有解释如何配置才能正确激活管理站点
【解决方案2】:

如果您通过 Apache 和 mod_wsgi 执行此操作,那么您没有遵循本教程。本教程告诉您使用开发服务器,原因很充分:使用 Apache,您需要在每次更改代码时重新启动它。开发服务器检测更改并为您重新启动。

【讨论】:

  • 从我上面的代码中可以看出,存在一些缩进问题。使用 Django 服务器找到这些比挖掘 apache 日志要容易得多。感谢您的提示,我现在可以按预期工作了。
【解决方案3】:

我遇到了同样的错误。即使我请求了 domain.com/admin,我也只得到了欢迎页面。不确定我们的错误是否是由于相同的来源,因为我正在使用 mod_fcgid 在 hostgator 上运行我的 django 站点。

无论如何,通过为 python 添加更具体的自定义路径解决了我的问题,一直到包含我的 wsgi.py 文件的目录。

我的 index.fcgi 文件是:

...
# Add a custom Python path. (optional)
sys.path.insert(0, "/home/*username*/django")

# Switch to the directory of your project.
...

现在是:

...
# Add a custom Python path. (optional)
sys.path.insert(0, "/home/*username*/django")
sys.path.insert(0, "/home/*username*/django/mysite")
sys.path.insert(0, "/home/*username*/django/mysite/mysite")

# Switch to the directory of your project.
...

我认为这是由于欢迎页面代码位于路径列表中比管理代码更靠近开头的路径。

【讨论】:

    【解决方案4】:

    正如@Daniel 所提到的,Apache 中的 mod_wsgi 默认情况下不会获取代码更改。但是,它可以配置为这样做。见:

    http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes

    【讨论】: