简体   繁体   English

Django-静态文件不起作用

[英]Django - static files is not working

I'm new to Django. 我是Django的新手。 I want to add my CSS file in template/index.html, but I don't know how to do it except in DEBUG mode. 我想将我的CSS文件添加到template / index.html中,但除了调试模式外,我不知道该怎么做。

Project name: mercury 项目名称:汞

App name: gamma 应用名称:gamma

/root/workspace/django/mercury/mercury/settings.py /root/workspace/django/mercury/mercury/settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/root/workspace/django/mercury/static/'

STATICFILES_DIRS = [
    '/root/workspace/django/mercury/bootstrap/dist/',
]

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

/root/workspace/django/mercury/gamma/templates/gamma/index.html /root/workspace/django/mercury/gamma/templates/gamma/index.html

<link href="{% static 'css/dashboard.css' %}" rel="stylesheet">

Full path of the originally dashboard.css file 原始dashboard.css文件的完整路径

/root/workspace/django/mercury/bootstrap/dist/css/dashboard.css

After running 'python manage.py collectstatic' /root/workspace/django/mercury/static/css/dashboard.css 运行'python manage.py collectstatic'后/root/workspace/django/mercury/static/css/dashboard.css

The output: 输出:

[20/Oct/2017 12:27:48] "GET /static/css/dashboard.css HTTP/1.1" 404 98

I'm missing something? 我想念什么吗?

I tried numerous things but didn't helped. 我尝试了很多事情,但没有帮助。

Thanks in advance! 提前致谢!

[Solution] [解]

This is what I did to resolve my problem for those who are curious: 这是我为好奇的人解决的问题:

Install Apache/Httpd + mod_wsgi 安装Apache / Httpd + mod_wsgi

$ yum install httpd mod_wsgi

Edit the htppd config 编辑htppd配置

/etc/httpd/conf.d/django.conf

Alias /static /root/workspace/django/mercury/static
<Directory /root/workspace/django/mercury/static>
    Require all granted
</Directory>

<Directory /root/workspace/django/mercury>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess gamma python-path=/root/workspace/django/mercury:/usr/lib/python2.7/site-packages/
WSGIProcessGroup gamma
WSGIScriptAlias / /root/workspace/django/mercury/mercury/wsgi.py

Disable debug mode in settings.py 在settings.py中禁用调试模式

DEBUG = False

Static files need to be served by your webserver. 静态文件需要由您的Web服务器提供。 The point of using static files is, that a webserver is faster to deliver the file, if it does not need to pass through django. 使用静态文件的要点是,如果不需要通过django,则Web服务器可以更快地交付文件。 So you need to make sure, that your webserver serves /root/workspace/django/mercury/static/ at the url /static/ . 因此,您需要确保您的网络服务器在/root/workspace/django/mercury/static/处提供/root/workspace/django/mercury/static/ /static/

In DEBUG mode you can serve static files with the code snippet posted by @sandeep-balagopal, so they work with manage.py runserver when you're debugging. 在调试模式下,您可以提供带有@ sandeep-balagopal发布的代码段的静态文件,以便在调试时将它们与manage.py runserver使用。

See the documentation for more details. 有关更多详细信息,请参见文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM