我一直使用PHP,最後決定離開這個洞穴,並探索其他腳本語言,如Django。django如何靜態工作
我仍然在django的項目結構中包裝頭,似乎無法弄清楚如何包含css,js和其他所謂的靜態文件。
我已經閱讀了django wiki,我知道我應該使用靜態引用這些文件。但爲了做到這一點,我需要在settings.py
中定義我的靜態目錄。
我注意到當我調用靜態{% 'filename.extension' %}
時,它將文件追加到settings.py
中定義的靜態目錄中。但是如果我理解這一切就是這樣。問題是我有一些HTML代碼試圖從localhost:8000/static/directory/filename.extension
獲取文件但由於python以不同的方式處理url,所以html代碼不會從這樣的url中獲取任何文件,因此我的css將不會被加載。
我settings.py
我的文件結構:
PassWard >
PassWard >
__pycache__ >
wardvault >
__pucache__ >
... (pycache stuff) ...
templates >
imports.html
menu.html
wardvault >
base.html
passwords.html
migrations >
... (migration stuff) ...
__init__.py
admin.py
admin.pyc
apps.py
models.py
models.pyc
tests.py
views.py
views.pyc
__init__.py
static >
font_awesome >
... (lots of font related files) ...
js >
jquery.min.js
imports.html
menu.css
menu.html
menu.js
reset.css
db.splite3
manage.py
我settings.py
樣子:
"""
Django settings for PassWard project.
Generated by 'django-admin startproject' using Django 1.9.8.
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: keep the secret key used in production secret!
SECRET_KEY = 't+*07vdh&h^c*2ito-9_ywo^[email protected]^gb$%6vy7m5vr_^3'
# 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',
'PassWard.wardvault',
]
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 = 'PassWard.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 = 'PassWard.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/'
我想靜態文件加載到passwords.html
內wardvault
。
我的HTML的這段看起來是這樣的:
<link rel="stylesheet" type="text/css" href="{% static '/static/menu.css' %}"/>
和{% static '/static/menu.css' %}
結果時,我只是把它單獨是/static/menu.css
。
我的問題是當我使用static
它是否只是將settings.py
中定義的內容附加到參數?如果是這樣,當STATIC_URL
設置爲/static/
時,第一個斜槓是否表示從項目根目錄或系統根目錄開始?另外我是我正確使用它?或者有什麼我失蹤?
隨意指出項目結構的不良做法。我仍然試圖把所有的東西都包裹起來。