Django REST如何在表单中呈现所选字段

时间:2016-07-08 15:57:09

标签: django-rest-framework

我是Django REST的新手,请帮忙,如何只从表单中渲染一些字段,所以现在我得到所有字段和相关表格,但是我需要只显示一些字段

查看:

class ProFormList(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = "accounts/page_user_profile.html"



def get(self, request, id):
    account = get_object_or_404(Account, id=id)
    serializer = AccountSerializer(account)
    return Response({'serializer': serializer, 'account':account})


def post(self, request, id):
    account = get_object_or_404(Account, id=id)
    serializer = AccountSerializer(account, data=request.data)
    if not serializer.is_valid():
        return Response({'serializer': serializer, 'account':account})
    serializer.save()
    return redirect('accounts:profile', id=account.id)

我的Html:

 <div class="tab-pane " id="tab_1_2">
    <form role="form" action="{% url 'accounts:proform' id=account.id %}" method="POST">
    {% csrf_token %}
    {% render_form serializer %}
    <div class="form-group">
    <div class="margiv-top-10">
                                                                                    <button type="submit" class="btn green"> Save Changes </button>
   <button type="" class="btn default"> Cancel </button>
   </div>
</div>

1 个答案:

答案 0 :(得分:0)

class LoginSerializer(serializers.Serializer):
    first_name = serializers.CharField(
        max_length=100,
        style={'placeholder': 'first name'}
    )
    last_name = serializers.CharField(
        max_length=100,
        style={'placeholder': 'last name'}
    )