Dango之视图函数

1.request对象

HTTPRequest对象就是咱们的视图函数的参数request

def home(request):
    print(request)  #<WSGIRequest: GET '/home/'>
    # print(dir(request)) 

请求相关的常用值

  • path_info 返回用户访问url,不包括域名
  • method 请求中使用的HTTP方法的字符串表示,全大写表示。
  • GET 包含所有HTTP GET参数的类字典对象
  • POST 包含所有HTTP POST参数的类字典对象
  • body 请求体,byte类型 request.POST的数据就是从body里面提取到的

示例:

#/home/  纯路径
print(request.path) 

 #/home/  纯路径
print(request.path_info)

#/home/?a=1&b=2  全路径(不包含ip地址和端口)
print(request.get_full_path()) 

 #请求头相关数据,是一个字典
print(request.META) 

 #一个字符串,表示请求使用的HTTP 方法。必须使用大写。
print(request.method) 

#一个类似于字典的对象,包含 HTTP GET 的所有参数
print(request.GET)

#一个类似于字典的对象,如果请求中包含表单数据,则将这些数据封装成QueryDict对象。
print(request.POST)

#能够拿到请求数据部分的数据(post,get没有)
print(request.body)  

#清除session 一般用于退出登录 注销
request.session.flush

2.HTTPResponse对象

redirect 重定向

from django.shortcuts import render,HttpResponse,redirect
def home(request):
    #return HttpResponse("字符串")
    #return render(request, 'xx.html') #页面
    return redirect('/index/')  #路径

urls中定义好路径(被访问的路径)(要跳转的路径)

from django.contrib import admin
from django.urls import path
from  app01  import   views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', views.home),
    path('index/', views.index),
]

views中设置跳转

def home(request):
    return  redirect('/index/')  #路径

def  index(request):
    return render(request, 'index.html') #页面

render可渲染html的变量

def  index(request):
    v = "我真的好想你"
    #模版渲染,这是在回复给浏览器之前做的事
    return render(request, 'index.html',{v:v}) 
<div>可是我还想你啊,小猪</div>
<div>{{ v }}</div>

3.FBV和CBV

视图(视图函数和视图类)

类视图 CBV

views.py

from django.shortcuts import render,HttpResponse,redirect


#先引入类视图模块用于继承
from django.views import View

class LoginView(View):
    #处理get请求
    def get(self,request):
        return render(request,'login.html')
    #处理post请求
    def post(self,request):
        print(request.POST)
        return  HttpResponse("登陆成功")

urls.py路由写法

path('login/', views.LoginView.as_view()),#根据请求选择方法

html

<form action="" method="post">
    用户名: <input type="text" name="username">
    密码  : <input type="text" name="pwd">
            <input type="submit">
</form>

4.视图加装饰器

views.py

def wrapper(func):
    def inner(*args,**kwargs):
        print(111)
        ret = func(*args,**kwargs)
        print(222)
        return ret
    return inner

@wrapper
def index(request):
    print('我还想你')
    return HttpResponse("又有何用")

给视图类加装饰器

方法一: 常用

from  django.views import  View
from  django.utils.decorators import method_decorator

class LoginView(View):
    #处理get请求
    @method_decorator(wrapper)
    def get(self,request):
        return render(request,'login.html')
    #处理post请求
    def post(self,request):
        print(request.POST)
        return  HttpResponse("登陆成功")

方式二: 不常用

@method_decorator(wrapper,name="get")
class LoginView(View):
    #处理get请求
    def get(self,request):
        return render(request,'login.html')
    #处理post请求
    def post(self,request):
        print(request.POST)
        return  HttpResponse("登陆成功")

原文地址:https://www.cnblogs.com/zdqc/p/11638139.html