常用请求
get和post
- require_http_methos装饰器
from django.views.decorators.http import require_http_methods
@require_http_methods(['GET', 'POST'])
def index(request):
return HttpResponse('index')
- require_GET装饰器
from django.views.decorators.http import require_GET
@require_GET
def index(request):
return HttpResponse('index')
- require_POST装饰器
from django.views.decorators.http import require_POST
@require_POST
def index(request):
return HttpResponse('index')
通过查看源码可以知道,require_GET 和 require_POST其实都是调用了require_http_methods这个方法
request
我们在之前说过在视图函数里必须要传的一个参数就是request,缺少它就会报错,那么这个request到底是什么。我们打印这个request:
<WSGIRequest: GET '/book/book_dict/'>
可以知道这个request是一个WSGIRequest对象,即Django在接受http请求后,会根据http携带的参数和报文信息创建一个WSGIRequest对象,并作为视图函数的第一个参数传过来,也就是request,这个参数包含了客户端传过来的所有信息。
WSGIRequest常用属性
- path:返回请求的路径,不包括参数
- methos:返回请求方式
- GET:返回可以通过GET请求获取的参数
- POST:返回可以通过POST请求获取的参数
- FILES:返回上传的文件
- COOKIES:返回所有的cookie
- session:返回session
- META:返回客户端发送上来的所有header信息
WSGIRequest常用方法
- is_secure():是否采用了https
- is_ajax():是否采用了ajax发送请求
- get_host():服务器的域名
- get_full_path():返回完整的路径
- get_raw_uri():返回请求的完整url
JsonResponse
可以将数据以Json格式返回给浏览器
@require_http_methods(['GET', 'POST'])
def json_response(request):
dict = {"username": "sleet", "gender": "male"}
list = ['xxx', 'yyy', 'zzz']
tuple = ('xxx', 'yyy', 'zzz')
# return HttpResponse(dict, content_type='application/json')
# return JsonResponse(dict)
# In order to allow non-dict objects to be serialized set the safe parameter to False.
return JsonResponse(tuple, safe=False)
如果数据是列表或者元组,需要将JsonResponse的safe属性设置为False
类视图
类视图不需要用装饰器,它内部可以重写get和post方法来指定请求方式
from django.views import View
class IndexView(View):
def get(self, request):
return HttpResponse('GET_AddBookView')
def post(self, request):
return HttpResponse('POST_AddBookView')
def http_method_not_allowed(self, request, *args, **kwargs):
return HttpResponse(f'您当前的请求方法是{request.method}, 该路径只支持用post请求')
http_method_not_allowed方法是请求的方法与你写的请求都不符的时候,就会进入这个函数
映射
path('', views.IndexView.as_view(), name='index')
as_view()方法的作用获取你请求的方式和请求地址,如果你请求的方式和请求地址不在允许范围内,就会报错。如果没有as_view(),也会报错。
ListView
将数据以列表的形式展现出来,多用于分页展示
from django.views.generic import ListView
class BookListView(ListView):
model = Book
template_name = 'book_list.html'
paginate_by = 10
context_object_name = 'books'
ordering = 'id'
page_kwarg = 'page'
def get_context_data(self, *, object_list=None, **kwargs):
context = super(BookListView, self).get_context_data(**kwargs)
return context
def get_queryset(self):
return Book.objects.filter(id__lte=60)
- model:指定这个列表是那个模型的
- template_name:指定这个列表使用哪个模板进行渲染
- paginate_by:每页展示的数据量
- context_object_name:指定这个列表在模板中的参数名
- ordering:指定排序方式
- page_kwarg:指定获取页数的变量名,默认是page
- get_context_data:获取上下文数据
- get_queryset:指定获取的数据
打印上面代码的context
- paginator: Paginator类
- page_obj:Page类
- is_paginated:是否分页
- object_list:数据列表
Paginator类
Paginator常用属性和方法
- count:总共有多少数据
- num_pages:总页数
- page_range:页面区间(如果有10页,则返回的是range(1,11))
Page类
Page常用属性和方法
- has_next():是否有下一页
- has_previous():是否有上一页
- next_page_number():下一页页码
- previous_page_number():上一页页码
- number:当前页码
- start_index():当前页码的第一条数据的索引
- end_index():当前页妈的最后一条数据的索引
注意next_page_number和previous_page_number如果没有下一页或上一页会报错,因此基于用户体验需要做一个异常捕获