一、DTL讲解:
1、DTL模板是一种带有特殊语法的html文件,这个HTML文件可以被Django编译,可以传递参数进去,实现数据动态化 。在编译完成后,生成一个普通的HTML文件,然后发送给客户端 。
2、在Django里通常在views.py文件中会定义视图函数,同时需要导入对应的库,导入库的命令通常是from django.shortcuts import render
from django.http import HttpResponse
其中上面一个render,中文名就是渲染的意思,也就是在views.py文件里定义的方法,会通过return render(request,'index.html',context=context)这样的方式将index.html文件进行渲染 。index.html里会调用视图文件views.py里定义的变量,定义的方法和定义的属性 。
二、示例讲解:
下面代码是对DTL的for,with等的讲解 。
1、首先要创建项目和创建App,进入文件目录“E:ITPythonPYTHON试验”里,在地址栏输入cmd,进入cmd命令提示符环境下依次输入以下命令:
django-admin startproject demo08
cd demo08
python manage.py startapp books
相当于创建了一个demo08项目,在项目下创建了一个books的app,代码图示例如下:
文章插图
2、进入pycharm,打开E:ITPYthonPYTHONdemo08这个文件夹 。
①先设置项目目录下E:ITPYthonPYTHON试验demo08demo08settings.py的代码,代码设置如下:
"""Django settings for demo08 project.Generated by 'django-admin startproject' using Django 2.2.1.For more information on this file, seehttps://docs.djangoproject.com/en/2.2/topics/settings/For the full list of settings and their values, seehttps://docs.djangoproject.com/en/2.2/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/2.2/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!SECRET_KEY = '@f%my2rhwsou3p9%8xr1^r)#7j1d=c)!6(%8yk$7rehy-rh%=^'# 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','books',#需将app名称存入]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','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 = 'demo08.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR,'templates')],#设置静态文件存放目录'APP_DIRS': True,#为True代表在设置的上面一行DIRS里如果找不到静态文件的话,会自动去app目录下寻找静态文件'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 = 'demo08.wsgi.application'# Database# https://docs.djangoproject.com/en/2.2/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'db.sqlite3'),}}# Password validation# https://docs.djangoproject.com/en/2.2/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# https://docs.djangoproject.com/en/2.2/topics/i18n/LANGUAGE_CODE = 'zh-Hans'#设置中文语言TIME_ZONE = 'Asia/Shanghai'#设置上海时区USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (css, JAVAScript, Images)# https://docs.djangoproject.com/en/2.2/howto/static-files/STATIC_URL = '/static/'
真正需要设置的只有4行,分别是①在INSTALLED_APPS里添加app文件名,即'books',
推荐阅读
- 花草茶的知识,花草茶的描述
- win10上帝模式大揭秘,体验深藏不露的神秘功能
- 吃玛卡的禁忌,饮用芙蓉花茶的5条禁忌
- Python开发环境配置
- Windows 10这10项基础设置,加速你的PC运转!
- 《清明上河图》是我国哪朝的绘画作品?
- Python协程还不理解?请收下这份超详细的异步编程教程
- 打造局域网搜索利器,everything的ETP服务器,让搜索结果秒出
- 看我怎么通过Python脚本给100台网络设备一键化下发相同的配置
- 健达奇趣蛋是什么梗?