GVKun编程网logo

Django-无法加载静态CSS文件(django静态文件加载不出来)

18

本文将介绍Django-无法加载静态CSS文件的详细情况,特别是关于django静态文件加载不出来的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于0

本文将介绍Django-无法加载静态CSS文件的详细情况,特别是关于django静态文件加载不出来的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于08-Django加载静态文件、css – heroku&django:服务器不加载静态文件、django css js 等静态文件、Django Gunicorn无法加载静态文件的知识。

本文目录一览:

Django-无法加载静态CSS文件(django静态文件加载不出来)

Django-无法加载静态CSS文件(django静态文件加载不出来)

我正在runserver本地计算机(Mac OS X)上运行Django的开发服务器(),无法加载CSS文件。

以下是settings.py中的相关条目:

STATIC_ROOT = ''/Users/username/Projects/mysite/static/''STATIC_URL = ''/static/''STATICFILES_DIRS = (''/Users/thaymore/Projects/mysite/cal/static'',)STATICFILES_FINDERS = (''django.contrib.staticfiles.finders.FileSystemFinder'',''django.contrib.staticfiles.finders.AppDirectoriesFinder'',#''django.contrib.staticfiles.finders.DefaultStorageFinder'',)INSTALLED_APPS = (# other apps ...''django.contrib.staticfiles'',)

在我的views.py中,我请求上下文:

return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))

在我的模板中,{{ STATIC_URL }}渲染正确:

<link type="text/css" href="{{ STATIC_URL }}css/main.css" />

变成:

<link type="text/css" href="/static/css/main.css"/>

文件实际所在的位置。我还collectstatic确保所有文件都已收集。

我的urls.py中也包含以下几行:

from django.contrib.staticfiles.urls import staticfiles_urlpatternsurlpatterns += staticfiles_urlpatterns()

我是Django的新手,所以可能缺少一些简单的知识-希望对您有所帮助。

答案1

小编典典

请仔细阅读以下内容:https
:
//docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

django.contrib.staticfilesINSTALLED_APPSsettings.py

DEBUG=False吗 如果是这样,则需要runserver使用--insecure参数进行调用:

python manage.py runserver --insecure

collectstatic与通过开发服务器提供文件无关。它用于将静态文件收集到一个位置,STATIC_ROOT以供Web服务器查找它们。实际上,collectstatic将您的STATIC_ROOT设置运行到某个路径STATICFILES_DIRS是一个坏主意。您应该仔细检查以确保CSS文件现在甚至存在。

08-Django加载静态文件

08-Django加载静态文件

1.css文件以及js文件要放在static目录下,static和templates属于同级目录

2.在Django项目的同名项目文件的setting.py中,最后添加静态文件夹static目录路径

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

3.在templates下的HTML文件中,如果要引用static下的静态文件,则添加一下代码

{% load staticfiles %}
<link rel="stylesheet" href="{% static ''静态css文件相对static的路径'' %}">
<script src="{% static ''静态js文件相对static的路径'' %}"></script>

4.setting.py中的INSTALLED_APPS中包含django.contrib.staticfiles以及DEBUG选项为True

css – heroku&django:服务器不加载静态文件

css – heroku&django:服务器不加载静态文件

昨晚我决定将我的django学校项目投入生产.为此,我使用了Heroku.一切都很顺利,但“最后的触摸”让我发疯.它似乎没有看到我的静态文件,如网站上的bootstrap css’es,我不知道为什么因为例如ball.jpg被加载,管理员css也被加载.如果有人能找到我在过去10小时内处理的问题,那将是一个救生员:)

你可以看到结果here

我的settings.py

from django.conf import settings

if not settings.DEBUG:
import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# Security WARNING: keep the secret key used in production secret!
SECRET_KEY = '*****'

# Security WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ["*"]

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO','https')


# Application deFinition

INSTALLED_APPS = (
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','crispy_forms','team','tournament','geoposition','emailing',)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.auth.middleware.SessionAuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','django.middleware.security.SecurityMiddleware',)

ROOT_URLconf = 'football.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates','Dirs': [os.path.join(BASE_DIR,'templates')],'APP_Dirs': True,'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},]

Wsgi_APPLICATION = 'football.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = settings.DATABASES
DATABASES['default'] =  dj_database_url.config()


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static asset configuration
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"media","static_root")
STATICFILES_Dirs = (
    os.path.join(BASE_DIR,"media_in_pro","static"),)


# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesstorage'


# MEDIA SETTINGS
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,"media_root")

# CRISPY FORM SETTINGS
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# django-geoposition
GEOPOSITION_MAP_OPTIONS = {
    'minZoom': 8,'maxZoom': 15,'center': {'lat': 53.13248859999999,'lng': 23.168840300000056 },}

GEOPOSITION_MAP_WIDGET_HEIGHT = 240

GEOPOSITION_MARKER_OPTIONS = {
    'cursor': 'move'
}

虽然Git推

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Using set buildpack heroku/python
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote: 
remote: -----> Preparing static assets
remote:        Running collectstatic...
remote:        Post-processed 'admin/js/admin/DateTimeShortcuts.js' as 'admin/js/admin/DateTimeShortcuts.140919a6a17e.js'
(...)
remote:        82 static files copied to '/app/media/static_root',82 post-processed.
remote: 
remote: 
remote: -----> discovering process types
remote:        procfile declares types -> web
remote: 
remote: -----> Compressing... done,48.6MB
remote: -----> Launching... done,v51
remote:        https://footballapp.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.

我的heroku日志–tail:

2015-11-15T20:37:12.426289+00:00 heroku[slug-compiler]: Slug compilation started
2015-11-15T20:37:12.426301+00:00 heroku[slug-compiler]: Slug compilation finished
2015-11-15T20:37:12.456467+00:00 heroku[web.1]: State changed from up to starting
2015-11-15T20:37:16.171136+00:00 heroku[web.1]: Starting process with command `gunicorn football.wsgi`
2015-11-15T20:37:16.794898+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2015-11-15T20:37:17.969513+00:00 app[web.1]: [2015-11-15 20:37:17 +0000] [3] [INFO] Shutting down: Master
2015-11-15T20:37:17.956039+00:00 app[web.1]: [2015-11-15 20:37:17 +0000] [9] [INFO] Worker exiting (pid: 9)
2015-11-15T20:37:17.956047+00:00 app[web.1]: [2015-11-15 20:37:17 +0000] [10] [INFO] Worker exiting (pid: 10)
2015-11-15T20:37:17.956935+00:00 app[web.1]: [2015-11-15 20:37:17 +0000] [3] [INFO] Handling signal: term
2015-11-15T20:37:18.765840+00:00 heroku[web.1]: Process exited with status 0
2015-11-15T20:37:18.579708+00:00 app[web.1]: [2015-11-15 20:37:18 +0000] [3] [INFO] Listening at: http://0.0.0.0:46020 (3)
2015-11-15T20:37:18.579713+00:00 app[web.1]: [2015-11-15 20:37:18 +0000] [3] [INFO] Using worker: sync
2015-11-15T20:37:18.588372+00:00 app[web.1]: [2015-11-15 20:37:18 +0000] [9] [INFO] Booting worker with pid: 9
2015-11-15T20:37:18.578828+00:00 app[web.1]: [2015-11-15 20:37:18 +0000] [3] [INFO] Starting gunicorn 19.3.0
2015-11-15T20:37:18.655018+00:00 app[web.1]: [2015-11-15 20:37:18 +0000] [10] [INFO] Booting worker with pid: 10
2015-11-15T20:37:19.993385+00:00 heroku[web.1]: State changed from starting to up
2015-11-15T20:37:26.757037+00:00 heroku[router]: at=info method=GET path="/" host=footballapp.herokuapp.com request_id=16bde5f5-98bd-48a0-bde4-269947950c15 fwd="178.42.56.94" dyno=web.1 connect=1ms service=114ms status=200 bytes=4597
2015-11-15T20:37:27.090478+00:00 heroku[router]: at=info method=GET path="/static_root/img/cartoon-soccer-6.3dda712041b4.jpg" host=footballapp.herokuapp.com request_id=726fdf17-a3bb-4edc-94ee-8e0291c4fb68 fwd="178.42.56.94" dyno=web.1 connect=0ms service=4ms status=200 bytes=24769
2015-11-15T20:37:27.314794+00:00 heroku[router]: at=info method=GET path="/static_root/bootstrap-3.3.5-dist/js/npm.ccb7f3909e30.js" host=footballapp.herokuapp.com request_id=d22d2938-6955-4c0a-9c30-71a2dc408106 fwd="178.42.56.94" dyno=web.1 connect=1ms service=5ms status=200 bytes=566
2015-11-15T20:37:27.299303+00:00 heroku[router]: at=info method=GET path="/static_root/bootstrap-3.3.5-dist/js/bootstrap.8015042d0b4a.js" host=footballapp.herokuapp.com request_id=afceaef7-b06c-4523-b855-f6966a2829fa fwd="178.42.56.94" dyno=web.1 connect=0ms service=3ms status=200 bytes=14380
2015-11-15T20:37:43.569729+00:00 heroku[router]: at=info method=GET path="/" host=footballapp.herokuapp.com request_id=ab185988-12f4-4b78-a7d7-53eae067e891 fwd="178.42.56.94" dyno=web.1 connect=0ms service=8ms status=200 bytes=4597
2015-11-15T20:37:43.769540+00:00 heroku[router]: at=info method=GET path="/static_root/img/cartoon-soccer-6.3dda712041b4.jpg" host=footballapp.herokuapp.com request_id=e01d8276-1220-45f8-b9b1-866cd5ab2e7d fwd="178.42.56.94" dyno=web.1 connect=0ms service=2ms status=200 bytes=24769
2015-11-15T20:37:44.086677+00:00 heroku[router]: at=info method=GET path="/static_root/bootstrap-3.3.5-dist/js/npm.ccb7f3909e30.js" host=footballapp.herokuapp.com request_id=3172e050-63d5-421d-b3b2-cca661739c34 fwd="178.42.56.94" dyno=web.1 connect=0ms service=1ms status=200 bytes=566
2015-11-15T20:37:44.064945+00:00 heroku[router]: at=info method=GET path="/static_root/bootstrap-3.3.5-dist/js/bootstrap.8015042d0b4a.js" host=footballapp.herokuapp.com request_id=06b09fae-36db-4ac2-9d8d-fe0a2b3cfb06 fwd="178.42.56.94" dyno=web.1 connect=0ms service=1ms status=200 bytes=14380
2015-11-15T20:38:54.229389+00:00 heroku[api]: Deploy b75838d by tofik1432@gmail.com
2015-11-15T20:38:54.229389+00:00 heroku[api]: Release v51 created by tofik1432@gmail.com
2015-11-15T20:38:54.375061+00:00 heroku[slug-compiler]: Slug compilation started
2015-11-15T20:38:54.375070+00:00 heroku[slug-compiler]: Slug compilation finished
2015-11-15T20:38:54.509049+00:00 heroku[web.1]: State changed from up to starting
2015-11-15T20:38:57.508059+00:00 heroku[web.1]: Starting process with command `gunicorn football.wsgi`
2015-11-15T20:38:59.282919+00:00 app[web.1]: [2015-11-15 20:38:59 +0000] [3] [INFO] Starting gunicorn 19.3.0
2015-11-15T20:38:59.349517+00:00 app[web.1]: [2015-11-15 20:38:59 +0000] [10] [INFO] Booting worker with pid: 10
2015-11-15T20:38:59.283351+00:00 app[web.1]: [2015-11-15 20:38:59 +0000] [3] [INFO] Listening at: http://0.0.0.0:57838 (3)
2015-11-15T20:38:59.283475+00:00 app[web.1]: [2015-11-15 20:38:59 +0000] [3] [INFO] Using worker: sync
2015-11-15T20:38:59.286946+00:00 app[web.1]: [2015-11-15 20:38:59 +0000] [9] [INFO] Booting worker with pid: 9
2015-11-15T20:38:59.734335+00:00 heroku[web.1]: State changed from starting to up
2015-11-15T20:39:00.743234+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2015-11-15T20:39:02.232759+00:00 app[web.1]: [2015-11-15 20:39:02 +0000] [9] [INFO] Worker exiting (pid: 9)
2015-11-15T20:39:02.232763+00:00 app[web.1]: [2015-11-15 20:39:02 +0000] [10] [INFO] Worker exiting (pid: 10)
2015-11-15T20:39:02.252311+00:00 app[web.1]: [2015-11-15 20:39:02 +0000] [3] [INFO] Handling signal: term
2015-11-15T20:39:02.297059+00:00 app[web.1]: [2015-11-15 20:39:02 +0000] [3] [INFO] Shutting down: Master

编辑:
我发现似乎没有加载我的include html到base.html,有人有这个问题吗?

最佳答案
我最近在Heroku上遇到过类似的问题.似乎Django不能单独为生产提供静态资产.我能够使用WhiteNoise代替静态服务.首先安装它:

$pip install whitenoise

确保将whitenoise添加到requirements.txt中.

现在将以下内容添加到wsgi.py中:

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

最后,在settings.py中配置whitenoise:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesstorage'

或者取决于您运行的Django和WhiteNoise的版本:

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesstorage'

另外,请确保在settings.py中正确配置了静态

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT,'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_Dirs = (
    os.path.join(PROJECT_ROOT,'static'),)

更多信息:https://devcenter.heroku.com/articles/django-assets

django css js 等静态文件

django css js 等静态文件

http://www.crazyant.net/2012/07/20/django1-4%E8%AE%BE%E7%BD%AE%E6%A8%A1%E6%9D%BF%E8%B7%AF%E5%BE%84%E5%92%8Ccssjsimage%E7%AD%89%E8%B7%AF%E5%BE%84%E7%9A%84%E6%96%B9%E6%B3%95/

http://tntcastle.net/2012/11/django-1-4-template-with-css/

时间又是浪费在这种没有意义但又不可避免的纠结问题上

貌似不能把路径规定为叫static = = 改成media就特么行了= =

还有那个

defsome_view(request):
    # ...
    returnrender_to_response(''my_template.html'',
                              my_data_dictionary,
                              context_instance=RequestContext(request))
里的 context_instance=RequestContext(request) 到底是什么= =

Django Gunicorn无法加载静态文件

Django Gunicorn无法加载静态文件

我正在尝试使用gunicorn和nginx部署django项目,但我需要一些帮助。当我编码gunicorn
myproject.wsgi:application时,我设法在localhost页面上看到我的网站,但没有任何CSS。为什么gunicorn不将我的CSS文件加载到项目的静态文件夹中?

Guinicorn_start脚本:https : //dpaste.de/TAc4
Gunicorn输出:https
://dpaste.de/C6YX

我们今天的关于Django-无法加载静态CSS文件django静态文件加载不出来的分享就到这里,谢谢您的阅读,如果想了解更多关于08-Django加载静态文件、css – heroku&django:服务器不加载静态文件、django css js 等静态文件、Django Gunicorn无法加载静态文件的相关信息,可以在本站进行搜索。

本文标签: