Django的管理员缺少css,图像等 - 无法在共享主机上正确设置静态文件 [英] Django's admin is missing css, images, etc - unable to properly set up static files on shared host

查看:161
本文介绍了Django的管理员缺少css,图像等 - 无法在共享主机上正确设置静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前感谢您的帮助。



我正在尝试让Django在Bluehost上工作。 Django的管理员网站已启用,可通过 http://www.my-domain.com/admin/。然而,整个管理站点看起来像纯粹的html,没有风格或图像 - 与使用Django自己的服务器(使用 $ python manage.py runserver )所看到的不同。 p>

在寻找解决方案之后,我尝试了以下内容:


  1. 设置正确的值对于我的settings.py中的 STATIC_ROOT STATIC_URL 。(见下面的代码)。

  2. 运行:

      $ python manage.py collectstatic 
    pre>

    (由于某种原因似乎将文件复制到我的项目的根文件夹,而不是我在settings.py中指定的静态/文件夹)


  3. 访问管理网站 - 仍然看起来像纯HTML。


以下是我的settings.py文件中的一个摘录:

  import os.path 
import sys

#(这里有更多的代码)

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

以下是我的public_html文件夹中的.htaccess文件如何显示:

  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]

请注意以下事项:




  • 我无法访问httpd.conf。

  • 每当我使用python manage.py collectstatic,它会复制我项目的根文件夹中的文件,而不是我的项目的静态文件夹(这是我在 STATIC_ROOT 中指定的文件夹)。我尝试将它们手动复制到此文件夹中,但管理员网站仍然看起来像纯HTML。



请让我知道,如果你需要任何额外的信息。



任何帮助将不胜感激:)



谢谢!


解决方案

在.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:

  1. Setting the right values for STATIC_ROOT and STATIC_URL in my settings.py (see code below).
  2. 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)

  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆