简体   繁体   English

Django静态文件在本地加载,但不在开发服务器上加载

[英]Django static files load locally but not on development server

I was able to get my static files (CSS) to load in my local development environment, however when I push the changes to my development server I am unable to load the CSS. 我能够将我的静态文件(CSS)加载到本地开发环境中,但是当我将更改推送到开发服务器时,我无法加载CSS。 My local environment is Mac OS 10.9.2 and my development server is running Ubuntu 12.04.4 x64. 我的本地环境是Mac OS 10.9.2,开发服务器正在运行Ubuntu 12.04.4 x64。

settings.py settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static/'),
)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#7h&rny3^hz&q6w-8$6k&+msh554$pz*tx@$lj(+dgctvuj2j%'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

AUTHENTICATION_BACKENDS = (

    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',

)

TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.request',
    'allauth.account.context_processors.account',
    'allauth.socialaccount.context_processors.socialaccount',
    'django.contrib.auth.context_processors.auth',

)

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pcatapp',
    'south',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'tastypie',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #'django.core.context_processors.csrf',
)

ROOT_URLCONF = 'pcat.urls'

WSGI_APPLICATION = 'pcat.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'db',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

SITE_ID = 1

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/' 

An excerpt from the page source: 页面摘录:

<link rel="stylesheet" href="/static/myapp/style.css">

My static folder is located at myapp/static/myapp/style.css . 我的静态文件夹位于myapp/static/myapp/style.css All help is appreciated, thanks. 感谢所有帮助,谢谢。

I had the same problem and dj-static provided a simple solution 我遇到了同样的问题, dj-static提供了一个简单的解决方案

$ pip install dj-static

In your wsgi.py file: 在您的wsgi.py文件中:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Good Luck 祝好运

When using djangos development server, I have found that this problem, and its solution normally lies in the URLS file (when DEBUG=True), something like this will allow you to use django to serve your static files: 当使用djangos开发服务器时,我发现此问题及其解决方案通常位于URLS文件中(当DEBUG = True时),类似这样的事情将使您可以使用django提供静态文件:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = patterns(
    '',
    ... url includes etc.
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This will mean that any urls serving from {{ STATIC_URL }} will be handled by django, here are the django docs on this https://docs.djangoproject.com/en/dev/howto/static-files/ 这意味着django将处理{{STATIC_URL}}中提供的所有url,以下是https://docs.djangoproject.com/en/dev/howto/static-files/上的django文档。

EDIT (Added solution for running under a webserver not django development server): 编辑(添加的解决方案用于在Web服务器而不是Django开发服务器下运行):

If you are running your project behind a webserver, and not using djangos runserver, then its probably best you dont use djangos static file finder, and instead use its static file collector and the webserver to serve static files, to do this, try this in your settings.py: 如果您是在网络服务器后面运行项目,而不是使用djangos runserver,那么最好不要使用djangos静态文件查找器,而是使用其静态文件收集器和网络服务器来提供静态文件,为此,请尝试您的settings.py:

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

then run ./manage.py collectstatic and ensure your webserver redirects requests from /static/ to the full path of your static files directory, eg in NGINX you might do.. 然后运行./manage.py collectstatic并确保您的网络服务器将请求从/static/重定向到静态文件目录的完整路径,例如在NGINX中。

location /static/ {
    alias   /var/www/myproject/static/;
}

And apache: 和阿帕奇:

Alias /static/ /var/www/myproject/static/

Hope this helps... 希望这可以帮助...

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

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