admin 管理员组文章数量: 887007
Django3创建模板
前提条件
Django3创建项目及应用
进入项目test01目录
1、创建templates文件夹
2、在templates下创建对应应用名称的user文件夹
3、进入templates/user下创建index.html模板文件
<html><head><title>用户列表</title></head><body><h1>{{title}}</h1>{%for i in list%}{{i}}<br>{%endfor%}</body>
</html>
4、设置查找模板的路径:打开test01/settings.py文件,设置TEMPLATES的DIRS值
"""
Django settings for test01 project.Generated by 'django-admin startproject' using Django 3.0.5.For more information on this file, see
.0/topics/settings/For the full list of settings and their values, see
.0/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 .0/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '70)@b^z0+^cdmzdw@m7z2y_g+la!o*eocvn^@qevlb-8^)ol0^'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','user',
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middlewaremon.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'test01.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates',# 设置查找模板的路径'DIRS': [os.path.join(BASE_DIR, '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 = 'test01.wsgi.application'# Database
# .0/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'db.sqlite3'),}
}# Password validation
# .0/ref/settings/#auth-password-validatorsAUTH_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
# .0/topics/i18n/# LANGUAGE_CODE = 'en-us'
# TIME_ZONE = 'UTC'# 本地化
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# .0/howto/static-files/STATIC_URL = '/static/'
编辑user/views.py文件
from django.shortcuts import render# Create your views here.
from django.http import HttpResponse
from django.template import loader,RequestContextdef index(request):# 1.获取模板# template = loader.get_template('user/index.html')# 2.定义上下文# context = RequestContext(request, {'title': '用户列表', 'list': range(10)})# 3.渲染模板# return HttpResponse(template.render(context))# render就是做的以上封装return render(request, 'user/index.html', {'title': '用户列表', 'list': range(10)})def index2(request):return HttpResponse("Hello Django.")
浏览器访问:http://127.0.0.1:8000/
本文标签: Django3创建模板
版权声明:本文标题:Django3创建模板 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732357579h1534879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论