如何在Django表单中将两个表单字段呈现为一个字段?

编程入门 行业动态 更新时间:2024-10-20 08:26:01
本文介绍了如何在Django表单中将两个表单字段呈现为一个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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表单中将两个表单字段呈现为一个字段?

本文发布于:2023-11-24 22:40:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   中将   两个   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!