关于django的get请求和post请求

Django请求

Request请求,在试图函数定义的过程当中有一个参数叫做request,这个参数就是用来接收来自于请求的信息,请求的信息包含两部分:

Header

Body

请求分为以下几种

Get 获取,向服务器请求资源 明文

Get请求以?开始,键=值 以&分割

            www.laobian.com?name=老边&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参数

 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、确认提交地址

通过form表达来定义,当然也可以通过js

<!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>

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())

总结get的方法

1、接收数据

request.GET可以接收前端传递过来的get请求的数据

request.GET是一个类字典对象

2、处理数据

增删改查

3、返回结果

locals

Django post请求步骤

1、使用render方法进行返回

Render方法的第一个参数必须是request,其他地方和render_to_response方法相似

2、在form表单的最上层添加{% csrf_token标签 %}

3、然后开始和get请求类似接受数据和处理数据的步骤

Html
<form action="" method="POST">
        {% csrf_token %}
        <p>
            <label>类型名称</label>
            <input type="text" name="name">
        </p>
        <p>
            <label>类型名称</label>
            <textarea name="description"></textarea>
        </p>
        <p>
            <input type="submit" value="注册">
        </p>
    </form>
    <P>
        {% for t in types %}
            <p>{{ t.label }}___{{ t.description }}</p>
        {% endfor %}
    </P>
def formExa(request):
    if request.method == "POST" and request.POST:
        name = request.POST.get("name")
        description = request.POST.get("description")
        Type.objects.create(label = name,description = description)
        types = Type.objects.all()
    return render(request,"formTem.html",locals())

猜你喜欢

转载自blog.csdn.net/weixin_44303465/article/details/89452566
今日推荐