【发布时间】:2019-02-03 12:27:16
【问题描述】:
我正在尝试检索实例的当前数据以引导表单进行更新。当我使用{{user_form.as_p}} 时,所有当前数据都会自动填充,但是用引导表单替换它似乎是空的。这是我的 python 和 html 文件:
views.py
def update_profile(request):
if request.method == 'POST':
user_form = RegisterForm(request.POST, instance=request.user)
if user_form.is_valid():
user_form.save()
return redirect('profile')
else:
user_form = RegisterForm(instance=request.user)
return render(request, 'blog/update_profile.html', {
'user_form': user_form,
})
update_profile.html
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="formGroupExampleInput">Name</label>
<div id="formGroupExampleInput">
<input class="form-control" name="first_name" maxlength="150" autofocus required="" id="id_first_name" type="text" placeholder="First name" >
</div>
...
<div class="form-group">
<button type="submit" class="btn btn-success" id="formGroupExampleInput5">Save changes</button>
</div>
</form>
forms.py
class RegisterForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
class Meta:
model = User
fields = ('first_name', 'last_name', 'username', 'email', 'password1', 'password2')
【问题讨论】:
-
你在哪里渲染表单?
-
... 或细化问题:您是否将 {{user_form.as_p}} 放入引导模板中?
-
渲染没问题我测试过了。当我使用
{{user_form.as_p}}时,检索到的所有数据除了密码。如果我使用{{user_form.first_name}},它会再次起作用。但我无法使用{{user_form.first_name}}在此输入标签<input class="form-control" name="first_name" maxlength="150" autofocus required="" id="id_first_name" type="text" placeholder="First name">中集成代码。 @WillemVanOnsem -
我可以使用但不能在输入标签中使用。
value="{{user_form.first_name }}"也不起作用。 @ger.s.brett -
用{{user_form.as_p}}替换输入标签
标签: python django django-forms bootstrap-4