Django請求與表

request請求

在試圖函數定義的過程當中有一個參數叫做request,這個參數就是用來接收來自於請求的信息,請求的信息包含倆部分:
header
body
請求分爲一下幾種:
get獲取,向服務器請求資源
get請求以?開始,鍵=值 以&分隔
www.dema.com?name=dema&age=18
{“name”:“老邊”,“age”:“18”}
post提交,方法用來進行實體傳輸
head 和 get 方法類似,只是不會返回響應的主體,通常用於確認url的有效性和資源更新的時間
put 上傳文件
delete 指定刪除某個元素
options 用於查詢url指定資源支持方法
trace 客戶端可以通過這種方法對請求消息的傳輸路徑進行追蹤
connect 方法要求和代理服務器通信時創建隧道,實現用隧道協議進行tcp協議通信
Django 默認在request參數當中封裝了post和get方法,其他方法如果想要實現,需要用試圖類來自定義。
請求的狀態碼:
200 請求成功
300 跳轉
400 失敗
404 請求不存在
500 錯誤,服務器錯誤

Django試圖的request參數

<p>+++++++++++++++++++++++request本身+++++++++++++++++++++++++++++++</p>
    {{ request }}
    <p>+++++++++++++++++++++++request方法+++++++++++++++++++++++++++++++</p>
    {{ method }}
    <p>+++++++++++++++++++++++request.POST方法+++++++++++++++++++++++++++++++</p>
    {{ request.POST }}
    <p>+++++++++++++++++++++++request.GET方法+++++++++++++++++++++++++++++++</p>
    {{ request.GET }}
    <p>+++++++++++++++++++++++request.FILES方法+++++++++++++++++++++++++++++++</p>
    {{ request.FILES }}
    <p>+++++++++++++++++++++++request.body方法+++++++++++++++++++++++++++++++</p>
    {{ request.body }}
    <p>+++++++++++++++++++++++request.path方法+++++++++++++++++++++++++++++++</p>
    {{ request.path }}
    <p>+++++++++++++++++++++++request.method方法+++++++++++++++++++++++++++++++</p>
    {{ request.method }}
    <p>+++++++++++++++++++++++request.get_host方法+++++++++++++++++++++++++++++++</p>
    {{ request.get_host }}
    <p>+++++++++++++++++++++++request.META+++++++++++++++++++++++++++++++</p>
{#    {{ request.META }}#}
    {% for key,value in request.META.items %}
        <p>{{ key }}    :    {{ value }}</p>
    {% endfor %}
    <p>+++++++++++++++++++++++request.META.OS+++++++++++++++++++++++++++++++</p>
    {{ request.META.OS }}
    <p>+++++++++++++++++++++++request.META.HTTP_USER_AGENT+++++++++++++++++++++++++++++++</p>
    {{ request.META.HTTP_USER_AGENT }}
    <p>+++++++++++++++++++++++request.META.HTTP_HOST+++++++++++++++++++++++++++++++</p>
    {{ request.META.HTTP_HOST }}
    <p>+++++++++++++++++++++++request.META.SERVER_PORT+++++++++++++++++++++++++++++++</p>
    {{ request.META.SERVER_PORT }}

Django form表單

web開發當中,大部分的數據是通過form表達來向服務器進行提交的
提交步驟:
1.確認提交地址
通過forn 表達來定義,當然也可以通過js

Title

1、form表單通過action確定提交的位置,不寫或者爲空代表提交到當前路由
在這裏插入圖片描述

2、Form表單通過method確認請求方式,不寫或者爲空代表get方式提交
在這裏插入圖片描述

3、form表單提交的時候,表單元素必須有name且唯一

Html
Name 用來傳參,作爲傳參的鍵(標識)   唯一
<input type=”text” value=”hello” name=”say_hell”>
{“say_hello”: “hello”}
 Id 用來鎖定元素  唯一
Document.getElementById(“hello”)
$(“#id”)
   Class 用來描述樣式,通常用於css,在js當做當中批量選擇	器。 不唯一
Document.getElementByClass(“hello”)
$(“.class”)
 
2、發起提交事件
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
 <form action="" method="">
  	  <p>
        <label>文章類型</label>
        <input type="text" name="types">
    </p>
    <p>
        <label>文章類型</label>
        <input type="submit" value="查詢">
    </p>
</form>
</body>
</html>

3、後端(views)處理數據

 from Article.models import Type

def formExa(request):
    if request.method == "GET" and request.GET:
        types = request.GET.get("types")
        A = Type.objects.filter(label = types).first()
        if A:
            articles = A.article_set.all()
        else:
            articles = ["沒有%s類型的文章"%types]
    else:
        types = "None Type"
    return render_to_response("formTem.html",locals())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章