模板就是前端的页面,Django把html源码写到模板文件中,然后通过特定方法渲染后交给客户端。

模板路径设置方法有两种,分别是在项目目录下设置以及在应用目录下设置。

模板查找顺序:优先在DIRS设置的目录下查找templates,如果没有并且 'APP_DIRS': True时,继续在注册的app文件下查找templates。

1.在项目目录下设置

1).在项目目录下新建templates文件夹
2).在项目目录下的setting.py中找到模板设置TEMPLATES,并进行配置"DIRS"
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',
],
},
},
]
3).在templates文件夹下创建teacher文件夹
4).在templates/teacher文件夹下新建teacher_login.html文件,并编辑网页
<body>
<h1 style="color:red">我是teacher应用的登录页面</h1>
<form>
<p>账号:<input type="text"></p>
<p>密码:<input type="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
5).在teacher应用文件夹编辑views.py,并使用render进行渲染

# 方法一
from django.template.loader import get_template
from django.http import HttpResponse def login(request):
tp=get_template('teacher/teacher_login.html') #获取新建的html文件
html=tp.render() #网页渲染,html为字符串
return HttpResponse(html) #方法二:快捷方式
from django.shortcuts import render def login(request)
return render(request, 'teacher/teacher_login.html')
6).在teacher应用文件夹编辑urls.py
from django.urls import path
from . import views urlpattern=[
path('login/', views.login)
]

2.在应用目录下设置

1)在student应用目录下新建templates文件夹
2)在项目目录的setting.py中找到模板设置TEMPLATES和INSTALLED_APP,并上传
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'student' #注册应用
]
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',
],
},
},
]
3).在student/templates文件夹下新建student文件夹
4).在student/templates/student文件夹下新建student_login.html文件,并编辑网页
<body>
<h1 style="color:red">我是student应用的登录页面</h1>
<form>
<p>账号:<input type="text"></p>
<p>密码:<input type="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
5).在student应用文件夹编辑views.py,并使用render进行渲染
from django.shortcuts import render

def login(request)
return render(request, 'student/student_login.html')
6).在student应用文件夹编辑urls.py
from django.urls import path
from . import views urlpattern=[
path('login/', views.login)
]

Django入门--模板路径配置及渲染的更多相关文章

  1. Django 03 模板路径、模板变量、常用的过滤器

    Django 03 模板路径.模板变量.常用的过滤器 一.模板路径 #1.在每个app下面添加一个templates文件 #2.在项目views.py里面第33行INSTALLED_APPS里面添加上 ...

  2. Django 02 url路由配置及渲染方式

    Django 02 url路由配置及渲染方式 一.URL #URL #(Uniform Resoure Locator) 统一资源定位符:对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是 ...

  3. 3/21 Django框架 模板路径及模板过滤器 1.模板路径查找

    3/21 Django框架 模板路径及模板过滤器 1.模板路径查找 先找settings.py里的TEMPLATES列表下的DIRS路径.如果APP_DIRS为True,还会到注册了的APP文件夹下依 ...

  4. django中url路由配置及渲染方式

    今天我们学习如何配置url.如何传参.如何命名.以及渲染的方式,内容大致有以下几个方面. 创建视图函数并访问 创建app django中url规则 捕获参数 路径转换器 正则表达式 额外参数 渲染方式 ...

  5. Django——3 模板路径 模板变量 常用过滤器 静态文件的使用

    Django 模板路径 模板变量 过滤器 静态文件的加载 模板的路径,有两种方法来使用 设置一个总的templates在大项目外面,然后在sittings的TEMPLATES中声明 在每一个APP中创 ...

  6. Django入门--模板标签、继承与引用

    一.模板标签 Django模板引擎提供的可以在模板中进行的各种逻辑操作,是函数调用的一种特殊形式,如循环.判断等功能,期语法规则为: {% tag %} content {% tag 参数1 参数2 ...

  7. Django入门--模板变量、过滤器及静态文件

    一.模板变量 我们登录页面后,在页面上会显示姓名等信息,姓名就是模板变量,用来显示登陆者的名字,Django对这些数据进行处理后,返回给前端页面,前端页面进行渲染. 1.模板变量语法规则 1)在htm ...

  8. 最全的Django入门及常用配置

    Django 常用配置 Django 安装 pipx install django x 为python解释器版本2 or 3 如果你想安装指定版本的django,使用pip install djang ...

  9. django静态文件路径配置

    在settings.py中加入 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] 即可在html中引用该目录下的静态文件 <!DOCT ...

随机推荐

  1. Android自己定义控件之轮播图控件

    背景 近期要做一个轮播图的效果.网上看了几篇文章.基本上都能找到实现,效果还挺不错,可是在写的时候感觉每次都要单独去又一次在Activity里写一堆代码.于是自己封装了一下.这里仅仅是做了下封装成一个 ...

  2. java中的移位操作

    java中的移位操作仅仅对; a = a << 2; System.out.println(a); System.out.println(Integer.toBinaryString(a) ...

  3. ScalaChina: Scala中文社区

    给大家推荐一个很常使用心的Scala中文社区 ScalaChina地址:http://scalachina.org/ 来自社区创建者的<我为什么想做ScalaChina>: http:// ...

  4. Thinking in Java:容器深入研究

    1.虚线框表示Abstract类,图中大量的类的名字都是以Abstract开头的,它们仅仅是部分实现了特定接口的工具,因此创建时能够选择从Abstract继承. Collections中的实用方法:挑 ...

  5. Error解决:Property&#39;s synthesized getter follows Cocoa naming convention for returning &#39;owned&#39;

    在项目中定义了以new开头的textField.结果报错: 先看我的源代码: #import <UIKit/UIKit.h> @interface ResetPasswordViewCon ...

  6. ubuntu下安装AndroidStudio

    近期将电脑的操作系统换成了ubuntu,对于不习惯win8/win10的人来说ubuntu确实是一个不错的选择,主要的软件都ok了,至于QQ什么的,大家能够去找wine版的,或者直接下载一个叫Cros ...

  7. fragment.setMenuVisibility setUserVisibleHint

    [Android]Fragment真正意义上的onResume和onPause 前言 Fragment虽然有onResume和onPause的,但是这两个方法是Activity的方法,调用时机也是与A ...

  8. 0x54 树形DP

    树形DP我只知道千万别写森林转二叉树慢的要死 没有上司的舞会 水!裸! #include<cstdio> #include<cstring> #include<cstdl ...

  9. Windows下Vim主题变更

    默认的好丑! 主题位置. 修改配置文件. 添加主题设置. 新的主题,很高端大气. set fileencodings=utf8,ucs-bom,cp936,big set fileencoding=u ...

  10. mvc:view-controller直接转发页面

    在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...