【问题标题】:Django admin access not granted未授予 Django 管理员访问权限
【发布时间】:2012-07-12 05:57:53
【问题描述】:

我正在尝试为我正在创建的项目运行 /admin。我在这里面临两个问题

1.当我将浏览器指向 127.0.0.1/admin/ 时,它说找不到页面,但是当我将浏览器指向 127.0.0.1 时,我会得到项目文件中的文件列表如何纠正此问题

2.每次重启apache都没有创建pyc文件??

 -rwxrwxrwx 1 root root  546 2012-07-11 15:45 manage.py
  -rwxrwxrwx 1 root root    0 2012-07-12 17:53 __init__.py
  -rwxrwxrwx 1 root root   34 2012-07-12 18:03 test
  -rw-r--r-- 1 root root  114 2012-07-12 18:03 __init__.pyc
   drwxr-xr-x 2 root root 4096 2012-07-12 18:20 tmp
  -rwxrwxrwx 1 root root 4489 2012-07-12 18:20 settings.py
  -rwxrwxrwx 1 root root  585 2012-07-12 18:34 urls.py

我已在 settings.py 文件中的已安装应用程序中包含django.contrib.admin,这也是下面的 urls.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('',
      # Examples:
      # url(r'^$', '{{ project_name }}.views.home', name='home'),
      # url(r'^{{ project_name }}/', include('{{ project_name }}.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))
  )

EDIT

我还没有创建任何模块..我应该创建一个模块然后访问管理员吗??

EDIT1

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

# This application object is used by the development server
# as well as any WSGI server configured to use this file.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

/etc/apache2/httpd.conf

  WSGIScriptAlias / /opt/mysite/wsgi.py
  WSGIPythonPath /opt/mysite

  <Directory /opt/mysite>
  <Files wsgi.py>
  Order deny,allow
  Allow from all
  </Files>
  </Directory>

【问题讨论】:

    标签: django django-admin django-templates django-urls


    【解决方案1】:

    在我看来,您的 apache 配置不正确。

    查看这些文档:

    https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/

    http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

    当然,您可以在开发服务器上启动 Django,看看您是否可以访问管理区域。


    在你的 django.wsgi 中试试这个:

    import os,sys
    
    #add your django project in PYTHONPATH
    sys.path.append('/home/django/project/path/')
    
    os.environ['DJANGO_SETTINGS_MODULE']='projectName.settings'
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    apache_django_wsgi.conf:

    Alias /uploads/ "pathTo/uploads/"
    <Directory "pathTo/uploads/">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>
    
    Alias /static/ "pathTo/sitestatic/"
    <Directory  "pathTo/sitestatic/">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>
    
    WSGIScriptAlias / "pathTo/apache/django.wsgi"
    <Directory "pathTo/apache/django.wsgi">
        Order deny,allow
        Allow from all
    </Directory>
    

    然后,在您的 /etc/apache2/httpd.conf(或它所在的任何位置)中

    Include "/pathTo/apache_django_wsgi.conf"
    

    【讨论】:

    • 我已经创建了 wsgi.py 文件并将其放置在 mysite 中,如文档中所述。请查看问题以进行编辑 1..
    • “我还没有创建任何模块”是什么意思?
    • 对不起,我在已安装的应用程序中包含了我的模块之一。有关详细信息,请参阅编辑 1
    • 一个问题:1.django.wsgi 2.apache_django_wsgi.conf究竟应该在哪里......我创建的项目位于/opt/tex/site
    • 我把它们放在 ./myProject/apache .. 所以你可以创建一个目录 /opt/tex/site/apache 并将它们放在那里
    【解决方案2】:

    单击http://127.0.0.1:8000/,您需要该端口。如果你也去http://127.0.0.1,你只会看到文件列表。你不用runserver

    【讨论】:

    • 对不起,我误解了。希望您能解决您的问题。
    猜你喜欢
    • 2019-09-17
    • 2012-03-15
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2020-09-04
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多