Django框架文件解析--views.py
views from django.shortcuts import render #从django的shortcuts快捷方式包,载入render供给模块 from booktest.models import BookInfo,HeroInfo # # shortcuts包中,render工具封装的内容,以及工作流程: # def my_render(request,template_path,context_dict={},): # """使用模板文件流程""" # # # 1. 通过项目文件中Setting加载模板文件目录,使用django中template模板包中的loader工具的get_template方法,加载模板文件,并使用变量接收. # temp = loader.get_template(template_path) # 在项目中, 会针对每个应用,在template模板文件夹内创建一个应用同名文件夹,放该应用的所有模板. # # # 2. 定义模板上下文: 给模板传递数据,使用和loader工具同包的,RequstContext响应上下文工具,传入request参数,以及字典内其他需要放入的数据,同样变量接收. # context = RequestContext(request, context_dict) # 如不需传值则留空 # # # 3. 模板渲染: 转化模板内变量, 产生标准的html内容. 使用模板返回的实例对象, 调用render方法,传入响应上下文中返回的对象.变量接收 # res_html = temp.render(context) # 返回页面的所有内容. # # # 4. 返回数据给浏览器. 视图调用结束, 使用HttpResponse返回页面内容. # return HttpResponse(res_html) from django.template import loader,RequestContext # Create your views here. from django.http import HttpResponse # 从django框架http包中导入HttpResponse网页相应模块. # 当url满足要求的时候,view则返回相应的内容. def index(request): # return HttpResponse('index') # my_render(request,"booktest/index.html") return render(request, "booktest/index.html", {"content":"这是来于数据库的内容", "list":list(range(1,10))} ) def index2(request): return HttpResponse('这是第二个首页!') # 创建新模块的流程: # 0. 定义视图 # 1. 定义url地址. # 2. 导入models相应模块, 传入数据. # 3. 创建模板 # 4. 使用render,传入request, 为模板添加上下文,结合数据渲染模板,返回给浏览器. # 5. 返回值. def show_books(request): books = BookInfo.objects.all() return render(request, "booktest/show_books.html", {'books':books}) def detail(request,bid): # 根据返回的id重新查询书籍 # book = BookInfo.objects.get(bid) # 此处需要用变量对id进行赋值, 只穿变量无从可知从何查找 book = BookInfo.objects.get(id=bid) heros = book.heroinfo_set.all() return render(request, "booktest/detail.html", { "heros":heros, "book":book, })
views
from django.shortcuts import render
#从django的shortcuts快捷方式包,载入render供给模块
from booktest.models import BookInfo,HeroInfo
# # shortcuts包中,render工具封装的内容,以及工作流程:
# def my_render(request,template_path,context_dict={},):
# """使用模板文件流程"""
#
# # 1. 通过项目文件中Setting加载模板文件目录,使用django中template模板包中的loader工具的get_template方法,加载模板文件,并使用变量接收.
# temp = loader.get_template(template_path) # 在项目中, 会针对每个应用,在template模板文件夹内创建一个应用同名文件夹,放该应用的所有模板.
#
# # 2. 定义模板上下文: 给模板传递数据,使用和loader工具同包的,RequstContext响应上下文工具,传入request参数,以及字典内其他需要放入的数据,同样变量接收.
# context = RequestContext(request, context_dict) # 如不需传值则留空
#
# # 3. 模板渲染: 转化模板内变量, 产生标准的html内容. 使用模板返回的实例对象, 调用render方法,传入响应上下文中返回的对象.变量接收
# res_html = temp.render(context) # 返回页面的所有内容.
#
# # 4. 返回数据给浏览器. 视图调用结束, 使用HttpResponse返回页面内容.
# return HttpResponse(res_html)
from django.template import loader,RequestContext
# Create your views here.
from django.http import HttpResponse
# 从django框架http包中导入HttpResponse网页相应模块.
# 当url满足要求的时候,view则返回相应的内容.
def index(request):
# return HttpResponse('index')
# my_render(request,"booktest/index.html")
return render(request,
"booktest/index.html",
{"content":"这是来于数据库的内容",
"list":list(range(1,10))}
)
def index2(request):
return HttpResponse('这是第二个首页!')
# 创建新模块的流程:
# 0. 定义视图
# 1. 定义url地址.
# 2. 导入models相应模块, 传入数据.
# 3. 创建模板
# 4. 使用render,传入request, 为模板添加上下文,结合数据渲染模板,返回给浏览器.
# 5. 返回值.
def show_books(request):
books = BookInfo.objects.all()
return render(request, "booktest/show_books.html", {'books':books})
def detail(request,bid):
# 根据返回的id重新查询书籍
# book = BookInfo.objects.get(bid)
# 此处需要用变量对id进行赋值, 只穿变量无从可知从何查找
book = BookInfo.objects.get(id=bid)
heros = book.heroinfo_set.all()
return render(request, "booktest/detail.html", {
"heros":heros,
"book":book,
})