Django - - - -视图层之视图函数(views)
视图层之视图函数(views)的文件中。 一定包含两个对象:
django.http <span style="color: #0000ff;">def<span style="color: #000000;"> current_datetime(request):
now =<span style="color: #000000;"> datetime.datetime.now() html = <span style="color: #800000;">"<span style="color: #800000;">It is now %s.<span style="color: #800000;">" %<span style="color: #000000;"> now <span style="color: #0000ff;">return HttpResponse(html)
视图函数,围绕着两个对象进行:HttpResponse和HttpRequest 1.HttpRequest request---->请求信息 属性: request.path
request.body #含所有请求体信息 是bytes类型
: 标签中
name属性的值,FILES中的每一个value同时也是一个标准的python字典对象,包含下面三个Keys:
。如果访问用户当前没有登陆,user将被初始化为django.contrib.auth.models.AnonymousUser的实例。你 可以通过user的is_authenticated()方法来辨别用户是否登陆: if req.user.is_authenticated();只有激活Django中的AuthenticationMiddleware 时该属性才可用 <span style="color: #ff0000;">request. <span style="color: #000000;"><span style="color: #ff0000;">session: <span style="color: #008000;">唯一可读写的属性,代表当前会话的字典对象;自己有激活Django中的session支持时该属性才可用request.GET.get('name')<span style="color: #ff0000;"> 拿到GET请求里name的值 <span style="color: #ff0000;">如果某个键对应有多个值,则不能直接用get取值,需要用getlist,如: <span style="color: #ff0000;">request.POST.getlist("hobby")
方法: |
注意:键值对的值是多个的时候,比如checkbox类型的input标签,select标签,需要用:
2.HttpResponse
HttpResponse---->相应字符串
对于HttpRequest请求对象来说,是由django自动创建的,但是,HttpResponse响应对象就必须我们自己创建。每个view请求处理方法必须返回一个HttpResponse响应对象。HttpResponse类在django.http.HttpResponse。
在HttpResponse对象上扩展的常用方法
1.render 函数
render(request,template_name[,context])
结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的 HttpResponse 对象。
参数:
request: 用于生成响应的请求对象。
template_name:要使用的模板的完整名称,可选的参数
context:添加到模板上下文的一个字典。默认是一个空字典。如果字典中的某个值是可调用的,视图将在渲染模板之前调用它。
content_type:生成的文档要使用的MIME类型。默认为DEFAULT_CONTENT_TYPE 设置的值。
status:响应的状态码。默认为200。</pre>
<div class="cnblogs_code">
django.shortcuts <span style="color: #0000ff;">def<span style="color: #000000;"> test(request):
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">index.html<span style="color: #800000;">') <span style="color: #008000;">#<span style="color: #008000;">向用户显示一个html页面
下面为render官方源码,可以看出render最后也是返回了一个HttpResponse给webserver
render(request,template_name,context=None,content_type=None,status=None,using== loader.render_to_string(template_name,context,request,using= HttpResponse(content,content_type,status)
细说render:
render方法主要是将从服务器提取的数据,填充到模板中,然后将渲染后的html静态文件返回给浏览器。这里一定要注意:render渲染的是模板,下面我们看看什么叫作模板:
Title
<{{ book.btitle }}
上面{%%}之间包括的就是我们要从数据库取出的数据,进行填充。对于这样一个没有填充数据的html文件,浏览器是不能进行渲染的,所以,对于上述{%%}之间的内容先要被render进行渲染之后,才能发送给浏览器。
下面举个例子:


<{{ book.btitle }}<span style="color: #0000ff;"></<span style="color: #800000;">ul<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">body<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">html<span style="color: #0000ff;">>
= BookInfo.objects.get(pk=id)
herolist == {: herolist}
render(request,,context)
2.redirect 函数
函数
- 来反向解析名称
可以返回一个永久的重定向。
函数。
方法来获取重定向的URL:
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python keyword">from<code class="python plain">django.shortcuts<code class="python keyword">import<code class="python plain">redirect
<div class="line number2 index1 alt1">
<div class="line number3 index2 alt2"><code class="python keyword">def<code class="python plain">my_view(request):
<div class="line number4 index3 alt1"><code class="python spaces"><code class="python plain">...
<div class="line number5 index4 alt2"><code class="python spaces"><code class="python functions">object<code class="python keyword">=<code class="python plain">MyModel.objects.get(...)
<div class="line number6 index5 alt1"><code class="python spaces"><code class="python keyword">return<code class="python plain">redirect(<code class="python functions">object<code class="python plain">)
</td>
</tr>
</table>
方法反向解析URL:
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python keyword">def<code class="python plain">my_view(request):
<div class="line number2 index1 alt1"><code class="python spaces"><code class="python plain">...
<div class="line number3 index2 alt2"><code class="python spaces"><code class="python keyword">return<code class="python plain">redirect(<code class="python string">'some-view-name'<code class="python plain">,foo<code class="python keyword">=<code class="python string">'bar'<code class="python plain">)
</td>
</tr>
</table>
传递要重定向的一个硬编码的URL
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python keyword">def<code class="python plain">my_view(request):
<div class="line number2 index1 alt1"><code class="python spaces"><code class="python plain">...
<div class="line number3 index2 alt2"><code class="python spaces"><code class="python keyword">return<code class="python plain">redirect(<code class="python string">'/some/url/'<code class="python plain">)
</td>
</tr>
</table>
也可以是一个完整的URL:
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python keyword">def<code class="python plain">my_view(request):
<div class="line number2 index1 alt1"><code class="python spaces"><code class="python plain">...
<div class="line number3 index2 alt2"><code class="python spaces"><code class="python keyword">return<code class="python plain">redirect(<code class="python string">'http://example.com/'<code class="python plain">)
</td>
</tr>
</table>
返回一个临时重定向。参数;,将返回一个永久的重定向:
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python keyword">def<code class="python plain">my_view(request):
<div class="line number2 index1 alt1"><code class="python spaces"><code class="python plain">...
<div class="line number3 index2 alt2"><code class="python spaces"><code class="python functions">object<code class="python keyword">=<code class="python plain">MyModel.objects.get(...)
<div class="line number4 index3 alt1"><code class="python spaces"><code class="python keyword">return<code class="python plain">redirect(<code class="python functions">object<code class="python plain">,permanent<code class="python keyword">=<code class="python color1">True<code class="python plain">)
</td>
</tr>
</table>
跳转(重定向)应用


-----------------------------------url(r<span style="color: #800000;">"<span style="color: #800000;">login<span style="color: #800000;">"<span style="color: #000000;">,views.login),url(r<span style="color: #800000;">"<span style="color: #800000;">yuan_back<span style="color: #800000;">"<span style="color: #000000;">,views.yuan_back),-----------------------------------<span style="color: #000000;">views.py
<span style="color: #0000ff;">def<span style="color: #000000;"> login(req):
<span style="color: #0000ff;">if req.method==<span style="color: #800000;">"<span style="color: #800000;">POST<span style="color: #800000;">"<span style="color: #000000;">:
<span style="color: #0000ff;">if 1<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;"> return redirect("/yuan_back/")
name=<span style="color: #800000;">"<span style="color: #800000;">yuanhao<span style="color: #800000;">"
<span style="color: #0000ff;">return</span> render(req,<span style="color: #800000;">"</span><span style="color: #800000;">my backend.html</span><span style="color: #800000;">"</span><span style="color: #000000;">,locals())
</span><span style="color: #0000ff;">return</span> render(req,<span style="color: #800000;">"</span><span style="color: #800000;">login.html</span><span style="color: #800000;">"</span><span style="color: #000000;">,locals())
<span style="color: #0000ff;">def<span style="color: #000000;"> yuan_back(req):
name</span>=<span style="color: #800000;">"</span><span style="color: #800000;">苑昊</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">return</span> render(req,locals())
-----------------------------------<span style="color: #000000;">login.html
<form action=<span style="color: #800000;">"<span style="color: #800000;">/login/<span style="color: #800000;">" method=<span style="color: #800000;">"<span style="color: #800000;">post<span style="color: #800000;">">
姓名 name=>
性别 name=>
邮箱 name=>
value=>
-----------------------------------用户{{ name }}你好
下面我们来看一个现象:


--------------------urls.py------------------------------urlpatterns =<span style="color: #000000;"> [
url(r<span style="color: #800000;">'<span style="color: #800000;">^admin/<span style="color: #800000;">'<span style="color: #000000;">,admin.site.urls),url(r<span style="color: #800000;">'<span style="color: #800000;">^login/<span style="color: #800000;">'<span style="color: #000000;">,url(r<span style="color: #800000;">'<span style="color: #800000;">^index/<span style="color: #800000;">'<span style="color: #000000;">,views.index,),<span style="color: #008000;">#<span style="color: #008000;"> url(r'^register/',views.register,name='reg'),
<span style="color: #000000;">
]
------------------view.py-------------------------------
<span style="color: #0000ff;">def<span style="color: #000000;"> login(request):
<span style="color: #0000ff;">if request.method==<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username=request.POST.get(<span style="color: #800000;">'<span style="color: #800000;">user<span style="color: #800000;">'<span style="color: #000000;">)
password=request.POST.get(<span style="color: #800000;">'<span style="color: #800000;">pwd<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if username==<span style="color: #800000;">'<span style="color: #800000;">yuan<span style="color: #800000;">' <span style="color: #0000ff;">and password==<span style="color: #800000;">'<span style="color: #800000;">123<span style="color: #800000;">'<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;"> return render(request,'index.html')
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">'<span style="color: #800000;">/index/<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">login.html<span style="color: #800000;">',{<span style="color: #800000;">'<span style="color: #800000;">info<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">账号或密码错误<span style="color: #800000;">'<span style="color: #000000;">})
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">login.html<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> index(request):
name=<span style="color: #800000;">'<span style="color: #800000;">yuan<span style="color: #800000;">'
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">index.html<span style="color: #800000;">',{<span style="color: #800000;">'<span style="color: #800000;">a<span style="color: #800000;">'<span style="color: #000000;">:name})
---------------login.html--------------------------------
登陆界面
---------------login.html--------------------------------
个人主页
hello,{{ a}}
首先,启动服务器后,我们进入login页面


正确输入姓名,密码后,此时执行redirect函数,结果如下

现在我们将redirect换成render,再重新走一遍看看,在login页面,正确输入姓名,密码后,结果如下:

细心的人会发现,用render函数执行后的,地址栏的地址没有变化,还是login,且页面上的{{a}}此时也没有被渲染,所以hello,后面没有内容显示!
对比render与redirect:
原因是
render: 只是返回页面内容,但是未发送第二次请求
redirect:发送了第二次请求,url更新

(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
相关内容
- VB6做了个简单的ListView内容导出函数
- Groovy hasProperty / respondsTo
- LeetCode - Is Graph Bipartite?
- golang中锁mutex的实现
- Perl XML :: LibXML $node-> findnodes($xpath)查找不应该的
- Binding使用的属性、DataContext上下文绑定必须使用的情况
- Delphi属性Property学习应用总结
- delphi – 我可以在我的应用程序中链接谷歌地图而无需支付费
- Delphi和.NET之父:安德斯·海尔斯(Anders Hejlsberg)
- delphi – 如何正确显示基于资源管理器的右键单击菜单