Django为什么不加载内置的静态文件?

时间:2018-10-16 10:58:55

标签: django django-templates

我在通过uWSGI hunky dory运行的Apache服务器上设置了Django实例。当我使用python manage.py runserver时,在本地计算机上一切看起来都很完美。不幸的是,当我将文件上传到预先准备好的生产服务器的那一刻,它拒绝加载Django的所有内置静态文件,并返回了404,我不知道为什么...

要查询的网址列为www.mysite.com/static/admin/css/fonts.css,与管理后端(css/base.csscss/dashboard.csscss/responsive.css)相似。起初,我认为这些软件包可能有问题,因此我强制重新安装了Django,但没有成功。仍未加载...

这是我的uWSGI配置.ini:

[uwsgi]
chdir = /path/to/top/level/mysite
module = mysite.wsgi:application
env = DJANGO_SETTINGS_MODULE=mysite.settings
master = true
pidfile = /path/to/project.pid
socket = 127.0.0.1:49152
; Dont use UNIX sockets as it confuses the proxy and alters the request 
: URL's. Current Apache version cant handle it.
; socket=/var/run/swerth_django.sock
processes = 5
harakiri = 20
post-buffering = 1
max-requests = 5000
vacuum = true
home = /path/to/top/level/Virtualenv/directory
daemonize = /path/to/uWSGI.log
env = LANG=en_US.UTF-8
enable-threads = true

我将静态根和url包括如下(注释是我自己的理解):

#The URL of which the static files in STATIC_ROOT directory are served
STATIC_URL = '/static/'

#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = '/home/swerth/public_html/swerth/static'

但是值得一提的是,我目前还没有任何静态文件。我那时还没有发展。我只想使用Django随附的模板。

编辑:

好,所以我做了一些研究,并设法缩小了范围(感谢@Alasdair指出Django文档中的特定页面)。看来我可以正常运行python manage.py collectstatic并将其导入所有需要的文件到www.mysite.com/static中。但是由于某种原因,django dosnt似乎正在“查看”文件,因为它们未在浏览器中呈现(找不到404)。

鉴于此,我假设它在settings.py中是我的配置?

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

#The URL of which the static files in STATIC_ROOT directory are 
#served
STATIC_URL = '/static/'

#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

我的Apache使用以下配置设置为反向代理:

RewriteEngine off
ProxyPreserveHost on
ProxyPass "/"  "uwsgi://127.0.0.1:49152/"
ProxyPassReverse "/" "uwsgi://127.0.0.1:49152/"

我做错了什么吗? (请注意,我努力避免不得不编辑HTML文件静态文件链接,因为我真的很想知道我做错了什么,因此不会再发生,因为Django应该可以直接从www.mysite.com/static开始提供服务)

编辑第2轮:

好吧,所以我按照建议进行了尝试,并尝试设置代理以排除静态目录并仅正常提供文件,并添加了一个别名以将URL映射到实际目录,但是它仍然无法正常工作。它似乎完全忽略了我的ProxyPass异常并将其发送给Django,即使我将其包含在不太具体的规则之上?有趣的问题是,当我为不太具体的/admin/指定/而不是仅ProxyPass时,它仅代理该URL到Django,但是,突然之间Django为Django启动了404 /admin/。此外,我尝试提供的静态文件被禁止使用403?!?!我当前的.conf文件如下所示:

Alias /static/ /home/swerth/public_html/swerth/static/

<Directory /home/swerth/public_html/swerth/static/>
    Require all granted
</Directory>

<Directory /home/swerth/public_html/swerth/swerth/>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

ProxyPreserveHost on
ProxyPass /whm-server-status/ !
ProxyPass /static/ !
ProxyPass /  uwsgi://127.0.0.1:49152/
ProxyPassReverse / uwsgi://127.0.0.1:49152/

1 个答案:

答案 0 :(得分:1)

这可能是因为您尚未设置Apache服务静态文件。尝试执行以下操作

  1. 转到“静态”文件夹,然后使用pwd获取该文件夹的绝对路径。您的情况下看起来像/home/swerth/public_html/swerth/static。另外,请确保运行Apache httpd进程的用户具有对静态文件夹的读取权限。
  2. 现在通过执行编辑您的VirtualHost

    sudo nano /etc/apache2/sites-available/000-default.confsudo nano /etc/apache2/sites-available/000-default.conf 如果您使用其他配置文件,请用自己的000-default.conf替换。

  3. 将以下内容添加到虚拟主机块:

<VirtualHost *:80>
  . . .

  Alias /static /home/swerth/public_html/swerth/static
  <Directory /home/swerth/public_html/swerth/static>
      Require all granted
  </Directory>

  . . .
</VirtualHost>

还要确保将其放在uwsgi <Directory>部分的上方 4.通过执行sudo systemctl restart apache2

重新启动apache

This是一个很好的教程,我发现了如何将Django应用程序部署到生产环境。

希望这会有所帮助。

相关问题