Django视图和模板

版权声明:一个苦逼初学者的原创小博客。 https://blog.csdn.net/SenZendao/article/details/82888746

Django是前端的一个框架用来连接网页和数据库的一个框架,也是比较简单的一个框架MVT,这篇就总结一下V和T,也就是视图和模板

视图 (views)

视图介绍
1.在django中,视图对WEB请求进行回应
2.视图接收reqeust对象作为第一个参数,包含了请求的信息
3.视图就是一个python函数,被定义在views.py中
4.定义完成视图后,需要配置urlconf,否则无法处理请求

******************views.py******************

from django.http import HttpResponse

def index(request):
    return HttpResponse("index")
def detail(request,id):
    return HttpResponse("detail %s" % id)

URLconf
1.在django中,定义URLconf包括正则表达式、视图两部分
2.django使用正则表达式匹配请求的URL,一旦匹配成功,则调用应用的视图
3.注意:只匹配路径部分,即除去域名、参数后的字符串
4.在test1/urls.py插入booktest,使主urlconf连接到booktest.urls模块
5.在booktest中的urls.py中添加urlconf

******************插入booktest的urls******************

在test1/urls.py插入booktest,使主urlconf连接到booktest.urls模块
url(r'^', include('booktest.urls’)),




******************添加urlconf******************

在booktest中的urls.py中添加urlconf

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.index),
    url(r'^([0-9]+)/$', views.detail),

模板

1.模板是html页面,可以根据视图中传递的数据填充值
2.创建模板的目录如下图:
在这里插入图片描述
3.修改settings.py文件,设置TEMPLATES的DIRS值

'DIRS': [os.path.join(BASE_DIR, 'templates')],

4.在模板中访问视图传递的数据

{{输出值,可以是变量,也可以是对象.属性}}
{%执行代码段%}

5.定义index.html模板
6.定义detail.html模板


*****************定义index.html模板*****************
<!DOCTYPE html>
<html>
<head>
  <title>首页</title>
</head>
<body>
<h1>图书列表</h1>
<ul>
{%for book in booklist%}
<li>
  <a href="{{book.id}}">
    {{book.btitle}}
  </a>
</li>
{%endfor%}
</ul>
</body>
</html>



*****************定义details.html模板*****************
在模板中访问对象成员时,都以属性的方式访问,即方法也不能加括号
<!DOCTYPE html>
<html>
<head>
  <title>详细页</title>
</head>
<body>
<h1>{{book.btitle}}</h1>
<ul>
  {%for hero in book.heroinfo_set.all%}
  <li>{{hero.hname}}---{{hero.hcontent}}</li>
  {%endfor%}
</ul>
</body>
</html>

使用模板
编辑views.py文件,在方法中调用模板

****************编辑views.py文件,在方法中调用模板****************
from django.http import HttpResponse
from django.template import RequestContext, loader
from models import BookInfo

def index(request):
    booklist = BookInfo.objects.all()
    template = loader.get_template('booktest/index.html')
    context = RequestContext(request, {'booklist': booklist})
    return HttpResponse(template.render(context))


def detail(reqeust, id):
    book = BookInfo.objects.get(pk=id)
    template = loader.get_template('booktest/detail.html')
    context = RequestContext(reqeust, {'book': book})
    return HttpResponse(template.render(context))

去除模板的硬编码

在index.html模板中,超链接是硬编码的,此时的请求地址为“127.0.0.1/1/”

<a href="{{book.id}}">

看如下情况:将urlconf中详细页改为如下,链接就找不到了

url(r'^book/([0-9]+)/$', views.detail),

此时的请求地址应该为“127.0.0.1/book/1/”

问题总结:如果在模板中地址硬编码,将来urlconf修改后,地址将失效
解决:使用命名的url设置超链接
修改test1/urls.py文件,在include中设置namespace

url(r'^admin/',include(admin.site.urls, namespace='booktest’)),

修改booktest/urls.py文件,设置name

url(r'^book/([0-9]+)/$', views.detail, name="detail"),

修改index.html模板中的链接

<a href="{%url 'booktest:detail' book.id%}">

Render简写

django提供了函数Render()简化视图调用模板、构造上下文

*****************render简写*****************

from django.shortcuts import render
from models import BookInfo

def index(reqeust):
    booklist = BookInfo.objects.all()
    return render(reqeust, 'booktest/index.html', {'booklist': booklist})


def detail(reqeust, id):
    book = BookInfo.objects.get(pk=id)
    return render(reqeust, 'booktest/detail.html', {'book': book})

总结

1.安装配置django运行的环境
2.编写模型,使用简单API与数据库交互
3.使用django的后台管理中维护数据
4.通过视图接收请求,通过模型获取数据,展示出来
5.调用模板完成展示

猜你喜欢

转载自blog.csdn.net/SenZendao/article/details/82888746