欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Django学习笔记(4):视图

程序员文章站 2022-07-14 22:39:37
...

1.视图的功能

接受请求,进行处理,与M和T进行交互,返回应答。
返回html内容HttpResponse,也可能重定向redirect

2.视图函数的使用

(1)定义视图函数
view.py

from django.http import HttpResponse,HttpResponseRedirect
# Create your views here.
def index(request):
    books = BookInfo.objects.all()
    return render(request,'booktest002/index.html',{'books':books})

(2)配置url
项目urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('booktest002.urls')),
]

应用的urls.py

urlpatterns = [
    url(r'^index$', views.index),
]

3.错误视图

自定义404页

settings.py中关闭调试模式

DEBUG = False

#ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']

在应用templates下面新建一个404.html,django会自动调用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404</title>
</head>
<body>
<h1>404</h1>
<h2>{{ request_path }}</h2>
</body>
</html>

Django学习笔记(4):视图

4.捕获url参数

位置参数

urls.py开启捕获参数

urlpatterns = [
    url(r'^index$', views.index),
    url(r'^create$',views.create),
    url(r'^delete(\d+)$',views.delete),//捕获参数
]

视图函数中第2个参数用来接受

def delete(request,index):
    return HttpResponse(index)

关键字参数

urlpatterns = [
    url(r'^index$', views.index),
    url(r'^create$',views.create),
    #url(r'^delete(\d+)$',views.delete),
    url(r'^delete(?P<index>\d+)$',views.delete)//关键字参数
]

视图函数中的参数名必须与urlpatterns中传的参数名一样

5.视图函数的request参数

requestHttpRequest类型的对象,包含浏览器请求的信息。

6.普通登录案例

request.POST 保存post方式提交的数据 QueryDict类型
request.GET 保存get方式提交的数据 QueryDict类型

QueryDict类型

类似于字典,但其允许一个键对应多个值。

>>> from django.http.request import QueryDict
>>> q = QueryDict('a=1&b=2&c=3')
>>> q['a']
'1'
>>> q['b']
'2'
>>> q.get('c')
'3'
>>> q.get('d','default')
'default'
>>> q1 = QueryDict('a=1&a=3&a=3')
>>> q1['a']
'3'
>>> q1.getlist('a')
['1', '3', '3']

login.html

<!DOCTYPE html>
<html>
<head>
	<title>登录</title>
	<style>
	*{
		margin:0;
		padding:0;
	}
	.box{
		width:100%;
		height:100%;
		text-align: center;
	}
	.inner_box{
		width:300px;
		height:200px;
		background-color: grey;
		margin:0 auto;
		padding-top:50px;
	}
	.input{
		width:200px;
		height:30px;
		line-height: 20px;
		font-size: 16px;
		display: block;
		margin:0 auto;
		margin-bottom: 10px;
	}
	.button{
		width:200px;
		height:30px;
		font-size: 16px;
		margin-top:20px;
	}
	</style>
</head>
<body>
<div class="box">
	<div class="inner_box">
		<form method="post" action="/login_check">
			<input class="input" type="text" name="username" placeholder="用户名">
			<input class="input" type="password" name="password" placeholder="密码">
			<input class="button" type="submit" value="登录" name="login" >
		</form>
	</div>
</div>
</body>
</html>

views.py

def login(request):
    return render(request,'booktest002/login.html')

def login_check(request):
    username = request.POST.get('username')
    password = request.POST.get('password')
    if(username=="admin" and password=="19990713"):
        return redirect('/index')
    else:
        return redirect('/login')

urls.py

urlpatterns = [
    url(r'^index$', views.index),
    url(r'^create$',views.create),
    #url(r'^delete(\d+)$',views.delete),
    url(r'^delete(?P<index>\d+)$',views.delete),
    url(r'^login$',views.login),
    url(r'^login_check$',views.login_check),
]

Django学习笔记(4):视图
Django学习笔记(4):视图