1

我尝试在 Windows 上执行 django (1.4) 教程管理员。但是无法访问 css 和 javascript。我在 setty.py 中设置:

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'C:/Python27/Lib/site-packages/django/contrib/admin/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/' 

我的 urils:从 django.conf.urls 导入模式、包含、url

取消注释接下来的两行以启用管理员:

从 django.contrib 导入管理员 admin.autodiscover()

urlpatterns = patterns('', # 示例:# url(r'^$', 'mysite.views.home', name='home'), # url(r'^mysite/', include('mysite.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)),

)

它仍然不起作用。您能否告诉如何解决这个问题以及解决它的最佳方法是什么?我的项目在 c:\django-projects\mysite 中。当我执行 adminhttp://127.0.0.1:8000/admin/polls/poll/1/我可以看到开发服务器的错误:

Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 68, in __call__
    return super(StaticFilesHandler, self).__call__(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 241, i
n __call__
    response = self.get_response(request)
  File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 58, in get_response
    return self.serve(request)
  File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 51, in serve
    return serve(request, self.file_path(request.path), insecure=True)
  File "C:\Python27\lib\site-packages\django\contrib\staticfiles\views.py", line
 41, in serve
    return static.serve(request, path, document_root=document_root, **kwargs)
  File "C:\Python27\lib\site-packages\django\views\static.py", line 57, in serve

    mimetype, encoding = mimetypes.guess_type(fullpath)
  File "C:\Python27\lib\mimetypes.py", line 294, in guess_type
    init()
  File "C:\Python27\lib\mimetypes.py", line 355, in init
    db.read_windows_registry()
  File "C:\Python27\lib\mimetypes.py", line 259, in read_windows_registry
    for ctype in enum_types(mimedb):
  File "C:\Python27\lib\mimetypes.py", line 249, in enum_types
    ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal

这是来自settings.py:

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',
    'polls'
)

在萤火虫中,我得到 500 个 css 和 js 文件的 http 返回码,路径对我来说似乎很奇怪。例如:

http://127.0.0.1:8000/static/admin/css/base.css

我尝试在settings.py中添加:

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    'C:/Python27/Lib/site-packages/django/contrib/admin/static/admin', //here css and js and img folders for for admin

)

并将 urls.py 更改为这个:

from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.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)),
)
urlpatterns += staticfiles_urlpatterns()

但它仍然不起作用。

Django 文档在这里说: https : //docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/#serving-the-admin-files :“管理文件位于(django/contrib/admin/ Django 发行版的静态/管理员)。”。但是还是不行。

4

0 回答 0