Django Apache/mod_python Admin CSS not appearing w

2019-03-11 06:50发布

I have Windows XP/Django/apache/mod_python working on localhost. All parts are working with the exception of the admin CSS not rendering. The admin works, but no html formatting. I've made additions in:

settings.py

  INSTALLED_APPS
  'django.contrib.admin',

urls.py

  from django.contrib import admin
  admin.autodiscover()
  (r'^admin/(.*)', admin.site.root),

conf/http.conf

  <Location "/"> 
    SetHandler python-program
    PythonPath "['C:/django'] + sys.path"
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
  </Location>

  <Location "/cpssite/"> 
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE myapplication.settings
    PythonInterpreter /myapplication
    PythonDebug On
  </Location>

I'm stumped. Is there more code I should have added somewhere?

5条回答
戒情不戒烟
2楼-- · 2019-03-11 06:53

Here is my django-specific apache configuration. Note, django handles every incoming url to the site (location /) except media, where it's disabled, and the data is served from django's media directory.

<Location "/">
  SetHandler python-program
  PythonHandler django.core.handlers.modpython
  SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  #PythonOption django.root /
  PythonDebug On
  PythonPath "['e:/dj'] + sys.path"
</Location>

Alias /media  e:/dj/django-trunk/django/contrib/admin/media/
<Location "/media">
  SetHandler None
</Location>
查看更多
老娘就宠你
3楼-- · 2019-03-11 07:01

Does your ADMIN_MEDIA_PREFIX exist? Is it different from MEDIA_URL? Did you include the trailing slash? Is Apache handled to correctly serve up the admin media?

The default Django configuration has the admin media located at {Django install dir}/contrib/admin/media. ADMIN_MEDIA_PREFIX defaults to /media/. So you need to add something like this to your Apache config:

Alias /media/ /path/to/django/contrib/admin/media/

This will tell Apache that requests for mysite.com/media/css/whatever.css mean to serve up /path/to/django/contrib/admin/media/css/whatever.css, which should solve your issue.

查看更多
甜甜的少女心
4楼-- · 2019-03-11 07:02

I used to have the same problem and the following entry in the http.conf worked fine with me:

<Directory "Path-to-python/Lib/site-packages/django/contrib/admin/media/"> 
    AllowOverride None 
    Options None 
    Order allow,deny 
    Allow from all 
</Directory> 

Alias /media/ "Path-to-Python/Lib/site-packages/django/contrib/admin/media/"

<Location "/mysite/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonOption django.root /mysite
    PythonInterpreter mysite
    PythonDebug On
    PythonPath "['C:/Python/Django/apps'] + sys.path"
</Location>
查看更多
Lonely孤独者°
5楼-- · 2019-03-11 07:05

Since the question is from long time back, this may not be a relevant answer but I am putting this information to help anyone who happens to stumble here just like me. As of version 1.4, ADMIN_MEDIA_PREFIX setting has been deprecated. The ways of serving static and media files with version >= 1.4 are described here

https://docs.djangoproject.com/en/dev/releases/1.4/#django-contrib-admin

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-files

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-the-admin-files

Basically it can be setup in 4 steps -

  1. Set STATIC_ROOT to point to directory which will serve all the static files of your site
  2. Set STATIC_URL for which static content should be served
  3. Run manage.py collectstatic
  4. Configure your webserver to serve requests for STATIC_URL from STATIC_ROOT

Same for media files

查看更多
何必那么认真
6楼-- · 2019-03-11 07:07

If you don't want to have admin media use the /media directory, you can specify ADMIN_MEDIA_PREFIX = 'admin_media', then create a link/alias from your webserver which redirects calls to /admin_media/ to the /usr/share/pyshared/django/contrib/admin/media (depending on your OS) for your production server...

查看更多
登录 后发表回答