其他分享
首页 > 其他分享> > 【django学习-05】视图之HttpRequest常见属性与方法

【django学习-05】视图之HttpRequest常见属性与方法

作者:互联网

#blog/urls.py
urlpatterns = [
    re_path('include/', views.include_ces),
    path('request_1/', views.request_ces),

]

#views.py
def request_ces(req):
    if req.method == "POST":
        #获取POST请求参数
        print(req.POST.get("user"))
        return HttpResponse("ok")
    #类方法
    print(req.is_secure())
    print(req.is_ajax())
    print(req.get_host())
    print(req.get_full_path())
    print(req.get_raw_uri())
    #类属性
    print(req.COOKIES)
    print(req.content_type)
    print(req.content_params)
    #获取get请求参数
    print(req.GET.get("xb"))
    return render(req,"HttpReq.html")

#HttpReq.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello world</h1>
<form action="/blog/request_1/" method="post">
    <input type="text" name="user">
    <input type="submit" value="submit">

</form>

</body>
</html>
False
False
127.0.0.1:8000
/blog/request_1/?xb=123
http://127.0.0.1:8000/blog/request_1/?xb=123
{}
text/plain
{}
123
xwl

标签:HttpRequest,请求,get,req,request,视图,print,path,05
来源: https://www.cnblogs.com/xwltest/p/16644753.html