0
私はDjango REST、pls help、formからいくつかのフィールドだけをレンダリングする方法が新しくなったので、今はすべてのフィールドと関連テーブルを取得しています。フィールド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>
はメタを使用する:あなたのシリアライザのフィールド – Jerzyk