Django-视图之HttpRequest对象

2022-09-05  本文已影响0人  测试探索

一、UTL路径参数

如果想从url中获取值http://127.0.0.1:8000/18/188/
子应用中book/urls.py

from django.urls import path
from book.views import create_book,shop

urlpatterns = [
    path('create/',create_book),
    path('list/<city_id>/<shop_id>/',shop),
]

子应用中book/views.py

from django.shortcuts import render
from django.http import HttpResponse
from book.models import BookInfo

# Create your views here.

def create_book(request):
    book = BookInfo.objects.create(
        name = 'abc',
        pub_data = '2000-1-1',
        readcount = 10
    )
    return HttpResponse('create')


def shop(request,city_id,shop_id):
    print("city_id",city_id)
    print("shop_id",shop_id)
    return HttpResponse('齐哥的饭店')
运行结果
运行理解流程图

二、查询字符串,形如key1=value1&key2=value2,Django中的QueryDict对象

HttpRequest对象的属性GET,POST都是QueryDict类型的对象,与python字段不同,QueryDict类型的对象用来处理同一个键带有多个值得情况。

get('键',默认值)
getlist('键',默认值)

eg:子应用中book/urls.py,对url:http://127.0.0.1:8000/11000/11004/?order=readcount&page=1&order=commentcount进行处理

from django.shortcuts import render
from django.http import HttpResponse
from book.models import BookInfo

# Create your views here.

def shop(request,city_id,shop_id):
    # 获取url传过来的参数
    # <QueryDict: {'order': ['readcount'], 'page': ['1']}>
    # QueryDict具有字典的特性,还具有一键多值
    query_params = request.GET
    print("query_params",query_params)
    order = query_params.getlist("order")
    print("order的请求参数为:",order)
    return HttpResponse('齐哥的饭店')

####################################
"""
http://ip:port/path/?key=value&key1=value1
url 以 ?为分割 分为2部分
?前边为 请求路径
?后边为 查询字符串 类似于字典 key=value 多个数据采用&拼接
"""
打印结果

三、POST请求,传递表单数据

postman构造好场景后,需要在settings.py文件中,中注释掉对post请求的校验,才可正常访问


image.png

在子应用中,book/views.py文件中在上一节基础上,新增register方法

from django.shortcuts import render
from django.http import HttpResponse
from book.models import BookInfo

# Create your views here.

def register(request):
    data = request.POST
    print("data",data)
    return HttpResponse('ok')

在子应用中,book/urls.py文件中,新增register的url

from django.urls import path
from book.views import create_book,shop,register

urlpatterns = [
    path('create/',create_book),
    path('<city_id>/<shop_id>/',shop),
    path('register/',register),
]

通过postman发送请求后,查看打印结果


image.png

四、传递json数据

postman构造场景


image.png

views.py文件中,增加方法

def jsons(request):
    # request.POST json数据不能通过该方式获取
    # 打印结果b'{\n    "name":"itcast",\n    "age":10\n}'
    body = request.body
    print("body",body)
    body_str = body.decode()
    print("body_str",body_str)
    # 字典数据
    body_dict = json.loads(body_str)
    print("body_dict",body_dict)
    print("请求头是:",request.META)
    return HttpResponse('json')

urls.py增加路由

from django.urls import path
from book.views import create_book,shop,register,jsons

urlpatterns = [
    path('create/',create_book),
    path('<city_id>/<shop_id>/',shop),
    path('register/',register),
    path('json/',jsons),

]

通过postman请求,得到打印结果


image.png
上一篇下一篇

猜你喜欢

热点阅读