【问题标题】:Django 1.8 static files doesnt workDjango 1.8 静态文件不起作用
【发布时间】:2015-07-09 13:23:08
【问题描述】:

我用的是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'),
)

【问题讨论】:

  • 请获取 BASE_DIR 的输出?注释 TEMPLATES 目录列表中的 DIRS 键
  • @cmidi BASE_DIR = /home/myUser/Project

标签: css django file static


【解决方案1】:

{% 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">

【讨论】:

  • 查看我的编辑。除了上述更改之外,请告诉我这是否有效。
  • 您仍然需要更改所有其他文件,但是您的 css 文件的语法错误 - 您需要关闭括号:&lt;link rel="stylesheet" href="{% static 'css/camera.css' %}"&gt; &lt;link rel="stylesheet" href="{% static 'css/stuck.css' %}"&gt; &lt;link rel="stylesheet" href="{% static 'css/style.css' %}"&gt;
  • 对我来说最后一次添加很有用:使用 {% static 'css/camera.css' %} 它开始工作。奇怪:在另一个 1.6 版本的 django 应用程序中,所有旧方法都有效!但是当我对 1.8.5 版本的新 django 应用程序重复代码时,它不起作用!请注意,旧的 1.6 应用程序使用新的 django 1.8.5。
【解决方案2】:

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

我把这个放在我的settings.py:

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

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

【讨论】:

    【解决方案3】:

    将 {% 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'),
    )
    

    【讨论】:

      【解决方案4】:

      如果BASE_DIR = /home/myUser/Project 是您的基本目录,那么就像您为模板网址添加“jetint”一样,您可以尝试将其添加到您的静态文件中

      STATIC_URL = 'jetint/static/'
      

      或者,更好的是,让你的BASE DIR = /home/myUser/Project/jetint

      【讨论】:

      • 不能放那个 BASE_DIR 因为是一个共享项目
      • 不,伙计,检查我的设置看看,我不知道发生了什么
      • 此时我能想到的最后一件事,你的 STATICFILES_DIRS 仍然是错误的。
      【解决方案5】:

      从 django.conf 导入设置

      STATIC_URL = settings.STATIC_URL
      
      def index(request):
          return render(request, '*.html', {'STATIC_URL':STATIC_URL})
      

      ''' 不好的方法 '''

      【讨论】:

        【解决方案6】:

        只需将其添加到您的 settings.py 中,并在与 manage.py 相同的位置添加 'static' 文件夹

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

        【讨论】:

          猜你喜欢
          • 2016-08-20
          • 2012-08-16
          • 1970-01-01
          • 2016-02-09
          • 1970-01-01
          • 2015-11-09
          • 1970-01-01
          • 2015-03-30
          • 2012-01-14
          相关资源
          最近更新 更多