[英]Django not loading static files (Pycharm)
我正在做一個需要在CSS樣式表和徽標文件上加載的項目。 但是我的Django服務器在加載這些文件時也返回了404資源找不到的信息。 我已經檢查了其他類似問題,但是沒有一個問題使用該格式指定我使用的靜態文件。 這是我正在使用的html代碼
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="/static/Emu86/style.css">
{% endblock stylesheets %}
這是我的settings.py文件:
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.9.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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.9/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# Application definition
INSTALLED_APPS = [
'Emu86',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/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.9/howto/static-files/
STATIC_URL = '/static/'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] \
%(message)s",
'datefmt': "%d%b%Y %H:%M:%S"
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'Emu86.log',
'formatter': 'verbose'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'propagate': True,
'level': 'DEBUG',
},
'Emu86': {
'handlers': ['file'],
'level': 'DEBUG',
},
},
}
項目結構:
Emu86/
mysite/
static/
Emu86/
您需要在setting.py中進行一些更改
加
STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ]
低於BASE_DIR...。
希望它能工作
{% load static %}
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{% static "Emu86/style.css"%}">
{% endblock stylesheets %}
我想路徑必須如上所述
您必須在模板頂部加載static
模板標記,然后使用它來獲取靜態文件的網址:
{% load static %}
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{% static "Emu86/style.css" %}">
{% endblock stylesheets %}
編輯
您必須在settings.py文件中定義STATICFILES_DIRS
常量,以從項目目錄中查找您的靜態文件夾:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "mysite", "static"),
]
首先,將此'django.template.context_processors.static'
添加到context_processors數組中。 然后在您的模板文件中使用這樣的靜態文件
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}Emu86/style.css">
**不要忘記將所有靜態文件放在靜態文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.