views.py
@login_required
def profile_edit(request):
profile, created = ProfileGroom.objects.get_or_create(user=request.user)
form = ProfileForm(request.POST or None, request.FILES or None,
instance=profile)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('profiles:profile', username=request.user.username)
context = {
"title": 'Edit Profile',
"form": form,
}
return render(request, 'profiles/profile_form.html', context)
profile_form.html
<form method='POST' action='{% url "profiles:profile_update" %}'
enctype='multipart/form-data' role="form">{% csrf_token %}
<div id="div_id_first_name" class="form-group"> <label for="id_first_name" class="control-label ">
First name
</label> <div class="controls "> <input class="textinput textInput form-control" id="id_first_name" maxlength="80" name="first_name" type="text" /> </div> </div> <div id="div_id_last_name" class="form-group"> <label for="id_last_name" class="control-label ">
Last name
</label> <div class="controls "> <input class="textinput textInput form-control" id="id_last_name" maxlength="80" name="last_name" type="text" /> </div> </div> <div id="div_id_age" class="form-group"> <label for="id_age" class="control-label ">
Age
</label> <div class="controls "> <input class="numberinput form-control" id="id_age" name="age" type="number" />
<input type='submit' class='btn btn-success' value='Add/Edit Profile' />
</form>
表单正在保存数据并正常工作但在打开以编辑数据时,它不会保留以前输入的数据。任何人都可以帮忙吗?谢谢。