Django 1.8静态文件不起作用

时间:2015-04-30 01:39:17

标签: css django file static

我使用的是1.8版本的Django和我的CSS,JS或图片无效,我真的不知道为什么,我的项目文件夹就像

Project
    Jetint
      Static
        CSS

我的设置:

    DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'clients',
    'front',
)

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 = 'jetint.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['jetint/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 = 'jetint.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bd',
        'USER': 'bd',
        'PASSWORD': 'xxxx',
        'HOST': '',
        'PORT': '',
        'ATOMIC_REQUESTS': True
    }
}

TEMPLATE_DIRS = (BASE_DIR, 'jetint/templates')

# 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 files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

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

STATICFILES_DIRS = (STATIC_ROOT,)

和我的base.html

<!DOCTYPE html>
<html lang="en">
<head>
<title> - {% block title-section%}{% endblock %}</title>
<meta charset="utf-8">
<meta name = "format-detection" content = "telephone=no" />
{# <link rel="icon" href="{{ STATIC_URL }}images/favicon.ico"> #}
{# <link rel="shortcut icon" href="{{ STATIC_URL }}images/favicon.ico" /> #}
{% load staticfiles %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/camera.css">
<link rel="stylesheet" href="{{ STATIC_URL }}css/stuck.css">
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">

但没有任何作用,css,js文件或图像。请帮忙吗?

编辑:

现在我的html看起来像这样:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Jet Internacional - {% block title-section%}{% endblock %}</title>
<meta charset="utf-8">
<meta name = "format-detection" content = "telephone=no" />
{# <link rel="icon" href="{{ STATIC_URL }}images/favicon.ico"> #}
{# <link rel="shortcut icon" href="{{ STATIC_URL }}images/favicon.ico" /> #}
<link rel="stylesheet" href="{% static 'css/camera.css' %">
<link rel="stylesheet" href="{% static 'css/stuck.css' %">
<link rel="stylesheet" href="{{ % static 'css/style.css'%">
<script src="{{ STATIC_URL }}js/jquery.js"></script>
<script src="{{ STATIC_URL }}js/jquery-migrate-1.1.1.js"></script>
<script src="{{ STATIC_URL }}js/script.js"></script>
<script src="{{ STATIC_URL }}js/superfish.js"></script>
<script src="{{ STATIC_URL }}js/jquery.equalheights.js"></script>
<script src="{{ STATIC_URL }}js/jquery.mobilemenu.js"></script>
<script src="{{ STATIC_URL }}js/jquery.easing.1.3.js"></script>
<script src="{{ STATIC_URL }}js/tmStickUp.js"></script>
<script src="{{ STATIC_URL }}js/jquery.ui.totop.js"></script>
<script src="{{ STATIC_URL }}js/camera.js"></script>
<!--[if (gt IE 9)|!(IE)]><!-->
<script src="{{ STATIC_URL }}js/jquery.mobile.customized.min.js"></script>
<!--<![endif]-->
<script src="{{ STATIC_URL }}js/jquery-ui-1.10.3.custom.js"></script>
<script>
 $(document).ready(function(){
        jQuery('#camera_wrap').camera({
            loader: false,
            pagination: false ,
            minHeight: '200',
            thumbnails: false,
            height: '25.625%',
            caption: false,
            navigation: true,
            fx: 'mosaic'
          });
        $().UItoTop({ easingType: 'easeOutQuart' });
        $('#stuck_container').tmStickUp({});
 }); 

 $(function() {
    jQuery( "#accordion" ).accordion({
       collapsible: true,
       heightStyle: "content",
    });
  });
</script>
<!--[if lt IE 8]>
 {% comment %}<div style=' clear: both; text-align:center; position: relative;'>
   <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
     <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." />
   </a>
</div>{% endcomment %}
<![endif]-->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<link rel="stylesheet" media="screen" href="css/ie.css">


<![endif]-->
</head>
<body class="page1" id="top">
<!--==============================
              header
=================================-->
<header>
<!--==============================
            Stuck menu
=================================-->
<section id="stuck_container">
  <div class="container">
    <div class="row">
      <div class="grid_12">
        <h1>
          <a href="/">
             <img src="{% static 'images/logo-rif.png' %}" width="200px;" alt="Logo alt">
          </a>
        </h1>
        <div class="header_socials"><a href="#" class="fa fa-facebook"></a><a href="#" class="fa fa-twitter"></a><a
                href="" CLASS="fa fa-instagram"></a>
               {#  <a href="{% url 'registo_usuario' %}"> Registro </a> #}
        </div>

和我的设置......

import os

BASE_DIR = 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 = 'm9y8koy5%cm4@_=sk_hjlbl$z%mgj*8dztu13@)^zr(j*g@q0m'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'clients',
    'front',
)

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 = 'jetint.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['jetint/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 = 'jetint.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bd',
        'USER': 'bd',
        'PASSWORD': '...',
        'HOST': '',
        'PORT': '',
        'ATOMIC_REQUESTS': True
    }
}

TEMPLATE_DIRS = (BASE_DIR, 'jetint/templates')

# 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 files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = 'jetint/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

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

6 个答案:

答案 0 :(得分:12)

{% load staticfiles %}放在html文档的最顶部。

还可以尝试:

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

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

- - - 编辑

此外,要在html模板中调用静态文件,请遵循以下格式:

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

答案 1 :(得分:4)

我遇到了同样的问题,但解决了它。

我把它放在我的settings.py

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

然后我重新启动了测试服务器。

答案 2 :(得分:2)

将{%load staticfiles%}放在html文档的最顶部。

像这样调用静态文件:

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

在设置中尝试使用STATIC_ROOT或STATICFILES_DIRS中的任何一个。

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

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

答案 3 :(得分:0)

如果BASE_DIR = /home/myUser/Project是您的BASE DIR,那么就像您为模板网址添加'jetint'一样,您可以尝试将其添加到您的静态像

STATIC_URL = 'jetint/static/'

或者,更好的是,制作BASE DIR = /home/myUser/Project/jetint

答案 4 :(得分:0)

来自django.conf导入设置

psql

&#39;&#39;&#39; 一个糟糕的方式 &#39;&#39;&#39;

答案 5 :(得分:0)

只需在settings.py中添加&#39;静态&#39;使用manage.py

在同一个地方的文件夹
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))