Django的管理员缺少css,图像等 - 无法在共享主机上正确设置静态文件 [英] Django's admin is missing css, images, etc - unable to properly set up static files on shared host
问题描述
提前感谢您的帮助。
在.htaccess文件中,您将过滤请求到^ / static /,因此您的重写规则将不适用。如果您复制静态文件位于 public_html / static /
,您应该看到admin css。
否则,如果要将请求指向静态文件目录
尝试删除
RewriteCond%{REQUEST_URI}!^ / static /
并添加一个规则来指向请求您的静态文件存在
RewriteRule ^(static /.*)$ path / to / static / files [L]
[L]
组件意味着最后,告诉apache不要以静态开始的路径运行其他规则。
Thanks in advance for your help.
I'm trying to get Django working on Bluehost. Django's admin site is enabled and accessible at http://www.my-domain.com/admin/. However, the whole admin site looks like plain html with no style or images - unlike what I see when using Django's own server (with $ python manage.py runserver
).
After looking around for a solution I tried the following:
- Setting the right values for
STATIC_ROOT
andSTATIC_URL
in my settings.py (see code below). Running:
$ python manage.py collectstatic
(which for some reason seems to copy the files to my project's root folder and not to the static/ folder I specified in settings.py)
Visiting the admin site - still looks like plain html.
Here's an extract from my settings.py file:
import os.path
import sys
# (some more code here)
PROJECT_ROOT = os.path.normpath(os.path.dirname(__ file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# empty
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
And here's how the .htaccess file in my public_html folder looks:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ my_fcgi_file.fcgi/$1 [QSA,L]
Note the following:
- I don't have access to httpd.conf.
- Whenever I use "python manage.py collectstatic" it copies the files in my project's root folder, not in my project's static folder (which is the folder I specified in
STATIC_ROOT
). I tried copying them manually into this folder, but the admin site still looks like plain html.
Please let me know if you need any additional information.
Any help will be much appreciated :)
Thanks!
In your .htaccess file you are filtering out requests to ^/static/, so your rewrite rules won't apply. If you copy your static files are at public_html/static/
, you should see the admin css.
Otherwise, if you want to point the requests to your static files directory
Try removing
RewriteCond %{REQUEST_URI} !^/static/
and adding a rule to point requests to where your static files live
RewriteRule ^(static/.*)$ path/to/static/files [L]
The [L]
component means last and is to tell apache not to run the other rules for paths that start with static.
这篇关于Django的管理员缺少css,图像等 - 无法在共享主机上正确设置静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!