Django TemplateView模板无数据显示问题求助
问题:Django模板无法显示详情数据
我是Django新手,尝试从列表中展示对象详情,代码逻辑正常,但详情数据无法在模板中显示。
视图代码
class PieceDetail(TemplateView): template_name = 'cabinets/piece_detail.html' def get_context_data(self, **kwargs): id = kwargs.get('pk') piece = get_object_or_404(Piece, pk=id) pprint(piece.pk) serializer = PieceSerializer(piece) pprint(serializer.data) return {'serializer': serializer, 'cabinets/piece_detail': piece.pk}
URL配置
path('piece_detail/<int:pk>/', views.PieceDetail.as_view(), name = 'piece_detail'),
HTML模板代码
{% extends "shared/base.html" %} {% load static %} {% block content %} </br> <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> <div class="btn-toolbar mb-2 mb-md-0"> <div class="btn-group me-2"> <a href="/cabinet/piece_list" ><button class="btn btn-primary"> Back </button></a> </div> </div> <br class="modal-body py-0"> <p>{{ serializer.data.id }}</p> <div class="container"> <div class="form-group"> {{ piece.description }} </div> <div class="form-group"> {{ piece.quantity }} </div> </div> </div> <p>The quantity shown is that usually used during construction of components</p> </div> {% endblock %}
终端输出(已获取到数据)
1 {'description': 'door rail', 'quantity': 2} [03/Nov/2023 12:16:05] "GET /cabinet/piece_detail/1/ HTTP/1.1" 200 14603
我尝试多种方法传递数据到模板均无效,改用APIView并给get_context_data添加request参数时,出现错误:"detail": "Method \"GET\" not allowed."。不清楚如何将序列化器数据传递到模板,求指出问题所在。
问题分析与修复方案
1. 模板无法获取数据的核心原因
- 视图的
get_context_data中,没有将查询到的piece对象传入模板上下文,模板里用{{ piece.description }}自然拿不到值。 - 上下文里返回的
'cabinets/piece_detail': piece.pk是无效键名(包含斜杠,不符合模板变量命名规则),完全无意义。 - 虽然传入了
serializer对象,但模板仅调用了serializer.data.id,未用它渲染description和quantity。
2. 修复视图代码
直接将piece对象传入上下文,TemplateView用于渲染模板,序列化器是API场景的工具,此处无需使用:
class PieceDetail(TemplateView): template_name = 'cabinets/piece_detail.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # 必须先调用父类方法 pk = kwargs.get('pk') piece = get_object_or_404(Piece, pk=pk) context['piece'] = piece # 将piece对象传入模板上下文 # 若确实需要序列化后的数据,可额外传入 # serializer = PieceSerializer(piece) # context['serializer_data'] = serializer.data return context
3. 调整模板代码
直接使用上下文里的piece对象渲染数据,同时修正模板语法错误:
{% extends "shared/base.html" %} {% load static %} {% block content %} <br> <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> <div class="btn-toolbar mb-2 mb-md-0"> <div class="btn-group me-2"> <a href="{% url 'piece_list' %}" ><button class="btn btn-primary"> Back </button></a> </div> </div> <div class="modal-body py-0"> <p>{{ piece.id }}</p> <div class="container"> <div class="form-group"> {{ piece.description }} </div> <div class="form-group"> {{ piece.quantity }} </div> </div> </div> <p>The quantity shown is that usually used during construction of components</p> </div> {% endblock %}
4. 关于APIView的错误
APIView是Django REST Framework用于构建API(返回JSON/XML等数据)的视图类,不能用来渲染HTML模板。你用它做模板渲染本身就是错误用法,出现Method "GET" not allowed是因为未在APIView中实现get方法,且即便实现,它也不会返回模板响应,所以不要用APIView处理模板渲染场景。
内容的提问来源于stack exchange,提问作者user2238003
相关产品推荐
相关产品推荐

