如何在Django表单中将两个表单字段呈现为一个字段? [英] How to render two form fields as one field in Django forms?
问题描述
我在Django应用程序上工作,我需要以不同的方式呈现表单。
I'm working on Django application where I need to render a form differently.
表单包含与一个人相关的多个字段。
The form contains multiple fields related to a person.
1. Firstname
2. Lastname
3. Email
4. Address
5. City
6. Country
7. Phone
8. Pincode
有是我的应用程序中的两个流程。在第一个流程中,我可以呈现所有表单字段,并在用户输入一些数据并提交时保存它们。
There are two flows in my application. In the first flow I can render all the form fields and save them when the user enters some data and submit.
但是在第二个流程中,我只需要显示三个
But in the second flow, I need to display only three fields as below.
1. Name - *Combination of Firstname and Lastname*
2. Email
3. Phone
在名称字段中输入的数据应以空格分隔-
并另存为名字和姓氏。对于表单中其他未显示的必填字段,我可以填充空值并将模型保存在后端中。
The data entered in the name field should be split by empty space - and saved as Firstname and Lastname. And for other remaining mandatory fields in the form that are not rendered, I can fill empty values and save the model in Backend.
呈现表单的最佳方法是什么
What is the best way to render the forms differently for each flow?
Python: 3.7.3
Django: 2.1.5
推荐答案
您可以使用发布/获取请求功能。例如:sudo代码
You can use Post / Get feature of request. For example: sudo code
def DataView(request):
if request.method == 'POST':
form = DataForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'users/form1.html', {'form': form})
else
ValidationError(_('Please login-in first'), code='invalid')
else:
#do your magic here. make a new form with limited fields or render this old form on another page where in html you can limit/modify the fields.
form = DataForm1()
return render(request, 'users/form1.html', {'form': form})
这篇关于如何在Django表单中将两个表单字段呈现为一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!