我正在创建一个允许用户编辑其个人资料中某些字段的Web应用程序。该应用程序使用Django表单来更新信息。但是,当用户输入新信息时,用户将被发送回他们的个人资料,但信息保持不变。以下是我尝试实现该功能的方法。
表格
class UpdateProfile(forms.ModelForm):
age = forms.IntegerField(required=False)
height = forms.IntegerField(required=False)
weight = forms.IntegerField(required=False)
class Meta:
#Here are the fields that i want editable
model = User
fields = ('age', 'height', 'weight')
视图
def update_profile(request):
args = {}
if request.method == 'POST':
form = UpdateProfile(request.POST, instance=request.user)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('account/profile.html'))
else:
form = UpdateProfile()
args['form'] = form
return render(request, 'account/edit_profile.html', args)
HTML
{% block body %}
<div class="container">
<form method="POST" action=".">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
<br>
</div>
{% endblock %}