django中Request获取数据和Serializer中获取数据总结

1.正则匹配的参数获取:url: P?<`count`>/?name=zhangsan&age=18&gender=1通过get(self,count):中指明re匹配的结果获取2.request.query_p
摘要由CSDN通过智能技术生成

1.正则匹配的参数获取:

url: P?<`count`>/?name=zhangsan&age=18&gender=1
通过视图函数中get(self,count):中指明count获得re匹配的结果

2.request.query_params:

得到一个QueryDict对象,使用QueryDict.get(‘name’)获取数据,也可以通过QueryDict.dict()方法将结果对象转换为python字典,只用于GET方式.

3.request.data:

得到的是请求体的数据,得到是一个dict,当请求为GET时候,获取结果为空
例如:post方式:

 name:张三,
 age:18,
 gender:1,

4.attrs:

用户传到serializer中validate验证时候,类型是一个OrderDict对象,获取数据的方式是OrderDict[‘key’]或者OrderDict.get(‘key’)使用后一种方式更安全,在没有key的情况下前者会报错,后者返回None

对于get<
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 使用serializerdjango template,可以通过调用drfserializer来渲染数据。首先,需要在view定义好serializer,并将查询到的数据序列化后传递给模板。例如: ```python from rest_framework import serializers class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = ['id', 'title', 'author', 'publish_date'] def book_list(request): books = Book.objects.all() serializer = BookSerializer(books, many=True) return render(request, 'book_list.html', {'books': serializer.data}) ``` 然后,在模板使用django的模板语言展示序列化后的数据: ```html {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} ``` 2. 使用Pagination 如果需要在django template使用drf的分页功能,可以通过调用Pagination类来实现。首先,需要在view定义好pagination,并将查询到的数据和pagination对象传递给模板。例如: ```python from rest_framework.pagination import PageNumberPagination class BookPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' max_page_size = 100 def book_list(request): books = Book.objects.all() pagination = BookPagination() result_page = pagination.paginate_queryset(books, request) serializer = BookSerializer(result_page, many=True) return render(request, 'book_list.html', {'books': serializer.data, 'paginator': pagination}) ``` 然后,在模板使用django的模板语言展示分页后的数据: ```html {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} <div class="pagination"> <span class="step-links"> {% if page_obj.has_previous %} <a href="?page=1">« first</a> <a href="?page={{ page_obj.previous_page_number }}">previous</a> {% endif %} <span class="current-page"> Page {{ page_obj.number }} of {{ paginator.num_pages }} </span> {% if page_obj.has_next %} <a href="?page={{ page_obj.next_page_number }}">next</a> <a href="?page={{ paginator.num_pages }}">last »</a> {% endif %} </span> </div> ``` 3. 使用Filter 如果需要在django template使用drf的过滤功能,可以通过调用FilterSet类来实现。首先,需要在view定义好filter,并将查询到的数据和filter对象传递给模板。例如: ```python from django_filters import rest_framework as filters class BookFilter(filters.FilterSet): title = filters.CharFilter(lookup_expr='icontains') author = filters.CharFilter(lookup_expr='icontains') class Meta: model = Book fields = ['title', 'author'] def book_list(request): books = Book.objects.all() book_filter = BookFilter(request.GET, queryset=books) serializer = BookSerializer(book_filter.qs, many=True) return render(request, 'book_list.html', {'books': serializer.data, 'filter': book_filter}) ``` 然后,在模板使用django的模板语言展示过滤后的数据: ```html <form method="get"> <label for="title">Title:</label> <input type="text" id="title" name="title" value="{{ filter.form.title.value }}"> <label for="author">Author:</label> <input type="text" id="author" name="author" value="{{ filter.form.author.value }}"> <button type="submit">Filter</button> </form> {% for book in books %} <div> <h3>{{ book.title }}</h3> <p>{{ book.author }}</p> <p>{{ book.publish_date }}</p> </div> {% endfor %} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值