Django 静态文件配置

来源:互联网 发布:区域网络管理 编辑:程序博客网 时间:2024/06/07 07:07

Django 在自身的开发服务器上有着自身的浅蓝色排版。部署到Apache服务器上会呈现一种无排版状态。

原因是 static 静态文件没有配置。


1. 在设置文件 settings.py 里,添加:

STATIC_URL = ‘/static/’

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

#other static file. can't include STATIC_ROOT

STATICFILES_DIR = (

os.path.join(BASE_DIR, 'common_static'),

);


STATICFILES_FINDERS = (

'django.contrib.staticfiles.finders.FileSystemFinder',

'django.contrib.staticfiles.finders.AppDirectoriesFinder',

);


2. 在app所在的工程里,新建 /static,/common_static,/media 文件夹


3. 然后在 Apache 的配置文件里,需要添加以下内容:

    Alias /media /path/to/media/
    <Directory /path/to/media>
            Require all granted
    </Directory>
    
    Alias /static /path/to/static/
    <Directory /path/to/static>
            Require all granted
    </Directory>
 
    WSGIScriptAlias / /path/to/prj/prj/wsgi.py
    <Directory /path/to/prj/prj>
    <Files wsgi.py>
            Require all granted
    </Files>
    </Directory>

4. cd /path/to/your project/manage.py 

python manage.py collectstatic


5. 重启Apache服务器,生效。


注:
如果用的是Apache2.2版本,用以下内容替换 Require all granted 赋予权限值:
    Order all, deny
    Allow from all

参考:http://www.ziqiangxuetang.com/django/django-static-files.html

0 0
原创粉丝点击