You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Postman请求获取所有书籍的表格形式返回结果?

问题解决步骤

核心问题分析

  1. 现有AddBookAPIView的GET方法仅生成空的Book实例,没有查询数据库中所有书籍
  2. 视图使用TemplateHTMLRenderer,返回的是添加书籍的表单模板,而非书籍列表数据,不适合Postman直接获取
  3. 模板内容是添加表单,完全没有展示书籍列表的逻辑

解决方案代码实现

1. 完善书籍序列化器(按需展示外键关联数据)

如果希望返回的书籍数据包含用户名、出版社名称而非ID,修改BookSerializer:

from rest_framework import serializers
from .models import Book
from django.contrib.auth.models import User

class BookSerializer(serializers.ModelSerializer):
    # 显示关联的用户名和出版社名称
    username = serializers.CharField(source='username.username')
    publisher = serializers.CharField(source='publisher.name')

    class Meta:
        model = Book
        fields = ['id', 'name', 'username', 'publication_date', 'publisher', 'description']

2. 新增获取所有书籍的视图

创建专门的视图,同时支持Postman获取JSON数据,以及网页端显示表格:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from .models import Book
from .serializers import BookSerializer

class BookListView(APIView):
    # 同时支持JSON和HTML渲染
    renderer_classes = [JSONRenderer, TemplateHTMLRenderer]
    template_name = 'book_list.html'

    @method_decorator(login_required(login_url='login/'))
    def get(self, request):
        # 查询所有书籍
        books = Book.objects.all()
        serializer = BookSerializer(books, many=True)
        
        # 根据请求头自动选择渲染方式:Postman请求默认返回JSON,浏览器请求返回HTML表格
        if request.accepted_renderer.format == 'html':
            return Response({'books': books})
        return Response(serializer.data)

3. 编写书籍列表表格模板(book_list.html)

<title>所有书籍</title>

<h3>书籍列表</h3>

<table border="1" cellpadding="8" cellspacing="0">
    <thead>
        <tr>
            <th>ID</th>
            <th>书名</th>
            <th>添加用户</th>
            <th>出版日期</th>
            <th>出版社</th>
            <th>描述</th>
        </tr>
    </thead>
    <tbody>
        {% for book in books %}
        <tr>
            <td>{{ book.id }}</td>
            <td>{{ book.name }}</td>
            <td>{{ book.username.username }}</td>
            <td>{{ book.publication_date }}</td>
            <td>{{ book.publisher.name }}</td>
            <td>{{ book.description }}</td>
        </tr>
        {% empty %}
        <tr>
            <td colspan="6">暂无书籍</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

4. 配置URL路由

在项目的urls.py中新增路由:

from django.urls import path
from .views import AddBookAPIView, BookListView

urlpatterns = [
    path('add-book/', AddBookAPIView.as_view(), name='add_book'),
    path('books/', BookListView.as_view(), name='book_list'),
    # 其他路由...
]

使用方法

  1. Postman获取JSON数据:发送GET请求到http://你的域名/books/,请求头默认即可,会返回所有书籍的JSON数组
  2. 浏览器查看表格:直接访问http://你的域名/books/,会显示格式化的书籍表格
  3. 注意:需要先登录,否则会跳转到登录页

内容的提问来源于stack exchange,提问作者Amir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 17:07:49