Django静态文件以及模板文件的配置

时间:2022-12-14 11:37:29

1、配置模板的路径

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
 1 TEMPLATES = [
2 {
3 'BACKEND': 'django.template.backends.django.DjangoTemplates',
4 'DIRS': [os.path.join(BASE_DIR, 'templates')]
5 ,
6 'APP_DIRS': True,
7 'OPTIONS': {
8 'context_processors': [
9 'django.template.context_processors.debug',
10 'django.template.context_processors.request',
11 'django.contrib.auth.context_processors.auth',
12 'django.contrib.messages.context_processors.messages',
13 ],
14 },
15 },
16 ]
配置模板的路径

 

2、配置静态文件的目录

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
 1 settings.py
2
3
4 # Static files (CSS, JavaScript, Images)
5 # https://docs.djangoproject.com/en/1.11/howto/static-files/
6
7 STATIC_URL = '/static/'
8
9 STAICFIES_DIRS =(
10 os.path.join(BASE_DIR,'static'),
11 )
12
13
14 login.html
15
16 <link rel="stylesheet" href="/static/commons.css"/>
17
18
19 <script src = '/static/jquery.min.js'></script>
配置静态文件的目录

 

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
1 body{
2 background: #eeeeee;
3 }
commons.css

 

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
 1 """S14Djngo URL Configuration
2
3 The `urlpatterns` list routes URLs to views. For more information please see:
4 https://docs.djangoproject.com/en/1.11/topics/http/urls/
5 Examples:
6 Function views
7 1. Add an import: from my_app import views
8 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9 Class-based views
10 1. Add an import: from other_app.views import Home
11 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12 Including another URLconf
13 1. Import the include() function: from django.conf.urls import url, include
14 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
15 """
16 from django.conf.urls import url
17 from django.contrib import admin
18
19 from cmdb import views
20
21
22
23 urlpatterns = [
24 url(r'^admin/', admin.site.urls),
25 #url(r'^h.html/',views.home),
26 url(r'^login',views.login),
27 ]
urls.py

 

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
 1 from django.shortcuts import render
2
3 # Create your views here.
4
5
6
7 from django.shortcuts import render
8
9 def login(request):
10
11 return render(request,'login.html')
views.py

 

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
  1 """
2 Django settings for S14Djngo project.
3
4 Generated by 'django-admin startproject' using Django 1.11.6.
5
6 For more information on this file, see
7 https://docs.djangoproject.com/en/1.11/topics/settings/
8
9 For the full list of settings and their values, see
10 https://docs.djangoproject.com/en/1.11/ref/settings/
11 """
12
13 import os
14
15 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17
18
19 # Quick-start development settings - unsuitable for production
20 # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21
22 # SECURITY WARNING: keep the secret key used in production secret!
23 SECRET_KEY = '^mh@&$v_x+3@+rz$ho$l^t^=tx-=b1a3ylc#nrnuh&mayil-4*'
24
25 # SECURITY WARNING: don't run with debug turned on in production!
26 DEBUG = True
27
28 ALLOWED_HOSTS = []
29
30
31 # Application definition
32
33 INSTALLED_APPS = [
34 'django.contrib.admin',
35 'django.contrib.auth',
36 'django.contrib.contenttypes',
37 'django.contrib.sessions',
38 'django.contrib.messages',
39 'django.contrib.staticfiles',
40 ]
41
42 MIDDLEWARE = [
43 'django.middleware.security.SecurityMiddleware',
44 'django.contrib.sessions.middleware.SessionMiddleware',
45 'django.middleware.common.CommonMiddleware',
46 'django.middleware.csrf.CsrfViewMiddleware',
47 'django.contrib.auth.middleware.AuthenticationMiddleware',
48 'django.contrib.messages.middleware.MessageMiddleware',
49 'django.middleware.clickjacking.XFrameOptionsMiddleware',
50 ]
51
52 ROOT_URLCONF = 'S14Djngo.urls'
53
54 TEMPLATES = [
55 {
56 'BACKEND': 'django.template.backends.django.DjangoTemplates',
57 'DIRS': [os.path.join(BASE_DIR, 'templates')]
58 ,
59 'APP_DIRS': True,
60 'OPTIONS': {
61 'context_processors': [
62 'django.template.context_processors.debug',
63 'django.template.context_processors.request',
64 'django.contrib.auth.context_processors.auth',
65 'django.contrib.messages.context_processors.messages',
66 ],
67 },
68 },
69 ]
70
71 WSGI_APPLICATION = 'S14Djngo.wsgi.application'
72
73
74 # Database
75 # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
76
77 DATABASES = {
78 'default': {
79 'ENGINE': 'django.db.backends.sqlite3',
80 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
81 }
82 }
83
84
85 # Password validation
86 # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
87
88 AUTH_PASSWORD_VALIDATORS = [
89 {
90 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
91 },
92 {
93 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
94 },
95 {
96 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
97 },
98 {
99 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
100 },
101 ]
102
103
104 # Internationalization
105 # https://docs.djangoproject.com/en/1.11/topics/i18n/
106
107 LANGUAGE_CODE = 'en-us'
108
109 TIME_ZONE = 'UTC'
110
111 USE_I18N = True
112
113 USE_L10N = True
114
115 USE_TZ = True
116
117
118 # Static files (CSS, JavaScript, Images)
119 # https://docs.djangoproject.com/en/1.11/howto/static-files/
120
121 STATIC_URL = '/static/'
122
123 STAICFIES_DIRS =(
124 os.path.join(BASE_DIR,'static'),
125 )
settings.py

 

Django静态文件以及模板文件的配置Django静态文件以及模板文件的配置
 1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6 <link rel="stylesheet" href="/static/commons.css"/>
7 <style>
8 label{
9 width:80px;
10 text-align:right;
11 display: inline-block;
12 }
13 </style>
14 </head>
15 <body>
16
17 <form action="/login" method="post">
18 <p>
19 <label for="username">用户名:</label>
20 <input id="username" type='text'/>
21 </p>
22 <p>
23 <label for="password">密码:</label>
24 <input id="password" type='text'/>
25 <input type="submit" value="提交"/>
26 </p>
27 </form>
28 <script src = '/static/jquery.min.js'></script>
29 </body>
30 </html>
login.html

 

Django静态文件以及模板文件的配置