Heroku静态文件不加载,Django [英] Heroku static files not loading, Django

查看:110
本文介绍了Heroku静态文件不加载,Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将推向Heroku,但它不是加载静态文件。



我用设置的东西,一切都很好,但我无法解决静态文件的问题。



我的目录结构是这样的

  help_the_needy 
help_the_needy
__init__.py
settings.py
网址。 py
views.py
wsgi.py
manage.py
Procfile
requirements.txt
static
css
font- awesome
字体
img
js
模板
base.html
display_list2.html
index.html

这里是完整的代码(所有文件)。



这个是我的settings.py。



我尝试了很多东西来解决这个问题,但似乎没有任何效果。



当我推送它时不会复制静态文件,但它是不加载它们。



有人可以请我指出我的错误吗?哪里出错?

解决方案

我也一直在处理同样的问题。这里是我在代码中改变的两件事。



(我使用的是Django 1.7)

1)settings.py

我将这些行添加到设置文件中

  BASE_DIR = os.path.dirname(os.path.dirname(__ file__))
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
STATICFILES_STORAGE ='whitenoise.django.GzipManifestStaticFilesStorage '
TEMPLATE_DIRS =(
os.path.join(BASE_DIR,'templates'),
#添加到此列表包含静态文件的所有位置

STATIC_ROOT:这告诉Django在哪里(a)在运行 python manage时放置静态文件.py collectstatic 和(b)当您运行应用程序时找到静态文件

TEMPLATE_DIRS:这告诉Django在哪里查找您的静态文件当您运行 python manage.py collectstatic



<2> wsgi.py

时搜索静态文件>

最初我的文件是:

  import os 
os.environ.setdefault(DJANGO_SETTINGS_MODULE, xxxx.settings)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我将它改为:

  import os 
os.environ。 setdefault(DJANGO_SETTINGS_MODULE,xxxx.settings)

from django.core.wsgi import get_wsgi_application $ b $ from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
应用程序= DjangoWhiteNoise(应用程序)

有关whitenoise的更多信息,请阅读此处: https://devcenter.heroku.com/articles/django-assets#whitenoise






另外,请记住安装whitenoise:
pip install whitenoise == 2.0.6



部署前该项目运行:
python manage.py collectstatic



这将创建一个由STATIC_ROOT (在settings.py中声明),包含所有静态文件。


I'm trying to push my Django project to Heroku, but it isn't loading the staticfiles.

I used this to setup the things, everything is fine but I'm not able to fix the issue with static files.

My directory structure is like this

help_the_needy
    help_the_needy
        __init__.py
        settings.py
        urls.py
        views.py
        wsgi.py
    manage.py
    Procfile  
    requirements.txt  
    static
        css
        font-awesome
        fonts  
        img  
        js
    templates
        base.html
        display_list2.html
        index.html

Here is the complete code (all files).

This is my settings.py.

I tried alot of things to fix this, but nothing seems to work.

When I push it does copy static files but it's not loading them.

Can someone please point me to my mistake? Where is it wrong?

解决方案

I have been dealing with the same problem too. And here are the 2 things that I changed in my code.

(I'm using Django 1.7)

1) settings.py

I add these lines to the setting files

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
    # Add to this list all the locations containing your static files 
)

STATIC_ROOT: this tells Django where to (a) put the static files when you run python manage.py collectstatic and (b) find the static files when you run the application

TEMPLATE_DIRS: this tells Django where to look for your static files when it search for statics files when you run python manage.py collectstatic

2) wsgi.py

Originally my file was:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And I changed it to:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Read here for more information on whitenoise: https://devcenter.heroku.com/articles/django-assets#whitenoise


Also, remember to install whitenoise: pip install whitenoise==2.0.6

Before deploying the project, run: python manage.py collectstatic

This will create a folder indicated by STATIC_ROOT (declared in your settings.py), containing all your static files.

这篇关于Heroku静态文件不加载,Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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