本文介绍了Django CSS附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过@Selcuk,我现在可以将其添加到 settings.py STATICFILES_FINDERS & STATICFILES_DIRS .当我推送到heroku网络应用程序后,该页面可在本地上使用样式表,但该样式表无法运行.

I have gotten it to now work locally thanks to @Selcuk by adding to the bottom of the settings.py STATICFILES_FINDERS & STATICFILES_DIRS. The page works on with stylesheet locally BUT once I push to my heroku web app it fails to run the stylesheet.

基本模板代码

<!DOCTYPE html>
<html>
<head>
        <title>ToolShare » Mohammad Daraghmeh</title>
        <!-- Fav Icon -->
        <link rel="shortcut icon" href="http://bit.ly/1xDbduz" type="image/x-icon">
            <link rel="icon" href="http://bit.ly/1xDbduz" type="image/x-icon">
        <!-- Latest compiled and minified CSS -->
        <link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/united/bootstrap.min.css" rel="stylesheet">
        <!-- Font-Awesome -->
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
        <!-- Animate -->
        <link href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.6/animate.min.css" rel="stylesheet">
        <!-- Latest jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
        <!-- Custom Css -->
        <link href="{{ STATIC_URL }}css/stylesheet.css" rel="stylesheet" type="text/css">

</head>

settings.py

"""
Django settings for toolshare project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS  = [os.path.join(BASE_DIR, 'templates')]
# 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 = '_0ut&uh=ou!jdw3e(ys@=x)k_)uu5d-if4ishx8lrjmiu_ma-q'

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

TEMPLATE_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',
    'toolshare',
)

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',
)

ROOT_URLCONF = 'toolshare.urls'

WSGI_APPLICATION = 'toolshare.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'EST'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    #"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

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

CMD的照片CSS的304正常吗,我想是的,因为它没有被修改.

Photo of CMDIs the 304 normal for a CSS, I am guessing yes for it is not being modifited.

推荐答案

您的 css 目录位于您的 static 目录下.您应该使用:

Your css directory is under your static directory. You should either use:

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

或放置

{% load staticfiles %}

在HTML文件顶部,然后使用

on top of your HTML file, then use

<link href="{% static 'css/stylesheet.css' %}" rel="stylesheet" type="text/css">

这篇关于Django CSS附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:01