【发布时间】:2021-10-25 15:19:10
【问题描述】:
我希望用户能够看到该字段的当前值是什么,同时提交另一个值
forms.py:
class CustomerInfoForm(forms.Form):
first_name = forms.CharField(
label="Firstname",
widget=widgets.TextInput(),
required=False,
)
last_name = forms.CharField(
label="Lastname",
widget=widgets.TextInput(),
required=False,
)
views.py:(通过电话号码认证)
@login_required
def customer_panel_info_view(request):
info_form = CustomerInfoForm(request.POST or None)
if request.user.is_authenticated:
user_phone_number = request.user.phone_number
if info_form.is_valid():
first_name = info_form.cleaned_data.get("first_name")
last_name = info_form.cleaned_data.get("last_name")
customer = User.objects.get(phone_number=user_phone_number)
customer.first_name = first_name
customer.last_name = last_name
customer.save()
context = {
"info_form": info_form,
}
return render(request, "panel/info.html", context)
模板:
<form action="" method="post">
{% csrf_token %}
{% info_form %}
<button type="submit" class="btn btn-success">submit</button>
</form>
这是流程: 用户进入此表单并想要添加、更改或删除一条信息(这是整个模板的一部分。实际上它包含性别生日和其他内容)。我希望字段具有当前值,以便用户知道哪些字段已经具有值
【问题讨论】:
-
为什么不使用 ModelForm?
标签: python django django-forms django-templates