找不到Django Static文件

时间:2017-02-22 16:15:34

标签: python css django

我正在使用Django框架的网站上工作。我已将项目相关的静态文件放在our_static中名为collectstaticstatic文件夹的文件夹中。以下是我的设置

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATICFILES_DIRS = [
     os.path.join(BASE_DIR, "our_static"), 
]

base.html文件:

{% load staticfiles %}    
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{% static 'style.css' %}" />
</head>

此处our_static文件根本没有被阅读。我的style.css位于our_static文件夹中。

编辑: 我使用AWS EC2 ubuntu 14.04作为我的服务器,该网站在localhost中工作正常但在AWS ubuntu服务器中没有。我正在使用Apache2服务器。

更多配置:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_FINDERS =[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

我的Apache2配置:

Alias /static /home/ubuntu/pythonserver/static
<Directory /home/ubuntu/pythonserver/static>
    Require all granted
</Directory>

<Directory /home/ubuntu/pythonserver/pythonserver>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess pythonserver python-path=/home/ubuntu/pythonserver python-home=/home/ubuntu/knowmenow/
WSGIProcessGroup pythonserver
WSGIScriptAlias / /home/ubuntu/pythonserver/pythonserver/wsgi.py

1 个答案:

答案 0 :(得分:0)

您的问题出在您的Apache配置中 - 可能在您的httpd.conf文件中。看看像https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7#configure-apache这样的任何教程,它将解释如何设置它。如果您使用httpd.conf,示例mod_wsgi文件将如下所示:

Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
    Require all granted
</Directory>

<Directory /home/user/myproject/myproject>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py

您还应该检查Apache错误日志的输出,这将有助于调试。他们通常在/var/log/apache2/error.log