我想在我的模板中只显示字符串变量中的消息,但我不知道如何显示。我使用的是Django Rest框架。我的代码:
表单.html
<p>{{ serializer.amount.errors }}</p>
序列化程序.py
from rest_framework import serializers
from .models import Data, Material
class DataSerializer(serializers.ModelSerializer):
class Meta:
model = Data
fields = ('order_date', 'material', 'amount', 'delivery_number', 'employee')
read_only_fields = ('id', 'insert_time')
extra_kwargs = {"amount": {"error_messages": {"invalid": "Test Message"}}}
class Form(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'zulieferung/form.html'
def get(self, request):
materials = Material.objects.distinct('material_unit_id')
return Response({'all_materials': materials})
def post(self, request):
materials = Material.objects.all()
serializer = DataSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'all_materials': materials}, status=status.HTTP_201_CREATED)
return Response({'serializer': serializer}, status=status.HTTP_400_BAD_REQUEST)
而不是
在我的模板中
[错误详细信息(string='Test Message',code='invalid')]