Python 如何根据用户输入(提交前)在django中动态更改表单
我正在做一个包含表单的网页,表单必须根据以前的用户输入(在提交之前)进行动态更改。例如,如果名称以字符串Python 如何根据用户输入(提交前)在django中动态更改表单,python,html,django,forms,dynamic,Python,Html,Django,Forms,Dynamic,我正在做一个包含表单的网页,表单必须根据以前的用户输入(在提交之前)进行动态更改。例如,如果名称以字符串S.L结尾,则需要自动跨越其余字段以介绍公司数据,否则必须使用默认值或任何值提交表单 表单继承自模型,所有内容都呈现为清晰的表单 在views.py中,我有: @login_required def form_home(request): if request.method == 'POST': Inputs = InputsForm(request.POST)
S.L
结尾,则需要自动跨越其余字段以介绍公司数据,否则必须使用默认值或任何值提交表单
表单继承自模型,所有内容都呈现为清晰的表单
在views.py中,我有:
@login_required
def form_home(request):
if request.method == 'POST':
Inputs = InputsForm(request.POST)
if Inputs.is_valid():
inp = Inputs.save(commit=False)
inp.author = request.user
inp.email = request.user.email
data = {'email': request.user.email, **Inputs.cleaned_data}
obtain_calculate_create_send(data)
inp.save()
messages.success(request, f'Your form is valid!')
return redirect('result/' + str(inp.pk) + '/')
else:
messages.warning(request, f'Please, check the inputs and enter valid ones')
content = {'Inputs': Inputs, 'title': 'valuations'}
else:
Inputs = InputsForm(request.POST or None)
content = {'Inputs': Inputs, 'title': 'valuations'}
return render(request, 'valuations/form_home.html', content)
在forms.py中:
class InputsForm(forms.ModelForm):
# Basic Info
country_choices = import_lists('Countries', equal=True)
years = import_lists('years')
name = forms.CharField(label='Company Name', initial='Example. S.L')
country = forms.TypedChoiceField(choices=country_choices, label='Country')
foundation_year = forms.TypedChoiceField(coerce=int, choices=years, label='Foundation year')
employees = forms.IntegerField(label='Number of employees')
industry = forms.TypedChoiceField(choices=industry_choices, label='Industry')
class Meta:
model = Inputs
exclude = ('author', 'email', 'date_inputs_created ')
在models.py中:
class Inputs(models.Model):
def __str__(self):
return f'{self.author}´s inputs for {self.name} created at {self.date_inputs_created}'
# Useful information for the data base
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_inputs_created = models.DateTimeField(auto_now_add=True)
email = models.EmailField(max_length=254, default='example@compani.com')
# Actual inputs
# Basic Info
# ---------
country_choices = import_lists('Countries', equal=True)
industry_choices = import_lists('Industry', equal=True)
name = models.CharField(max_length=100)
country = models.CharField(max_length=30, choices=country_choices, default='Germany')
foundation_year = models.PositiveSmallIntegerField(choices=years)
employees = models.PositiveIntegerField(default=20)
industry = models.CharField(max_length=50, choices=industry_choices)
在HTML中:
{% block content %}
<form method="post">
{% csrf_token %}
{{ Inputs|crispy }}
<button type="submit" class="btn btn-primary">submit</button>
</form>
{% endblock %}
{%block content%}
{%csrf_令牌%}
{{Inputs | crispy}}
提交
{%endblock%}
感谢您提供的任何帮助。为了让您了解如何改变它,我认为可以通过三种方式实现
def save(self, *args, **kwargs):
name= self.name
if name.endswith("S.L"):
self.country = 'Company country'
self.foundation_year = 'Company year'
.... And so on ...........
super().save(*args, **kwargs)
上述代码的工作原理是,对于每个传入数据,它检查名称是否以“S.L”结尾,并更改逻辑
您可以更改视图逻辑并应用此条件,但如果不想更改当前视图定义,则可以这样做
如果你想尝试选项2,那么,试试这个
if request.method == 'POST':
Inputs = InputsForm(request.POST)
if Inputs.is_valid():
inp = Inputs.save(commit=False)
inp.author = request.user
inp.email = request.user.email
# CODE -----------------------
name = inp.name
if name.endswith("S.L"):
inp.country = 'Company country'
inp.foundation_year = 'Company year'
.... And so on ...........
#CODE ! ----------------------
data = {'email': request.user.email, **Inputs.cleaned_data}
obtain_calculate_create_send(data)
inp.save()
messages.success(request, f'Your form is valid!')
return redirect('result/' + str(inp.pk) + '/')
else:
messages.warning(request, f'Please, check the inputs and enter valid ones')
content = {'Inputs': Inputs, 'title': 'valuations'}
else:
Inputs = InputsForm(request.POST or None)
content = {'Inputs': Inputs, 'title': 'valuations'}
return render(request, 'valuations/form_home.html', content)
为了让你了解如何改变它,我认为可以通过三种方式实现
def save(self, *args, **kwargs):
name= self.name
if name.endswith("S.L"):
self.country = 'Company country'
self.foundation_year = 'Company year'
.... And so on ...........
super().save(*args, **kwargs)
上述代码的工作原理是,对于每个传入数据,它检查名称是否以“S.L”结尾,并更改逻辑
您可以更改视图逻辑并应用此条件,但如果不想更改当前视图定义,则可以这样做
如果你想尝试选项2,那么,试试这个
if request.method == 'POST':
Inputs = InputsForm(request.POST)
if Inputs.is_valid():
inp = Inputs.save(commit=False)
inp.author = request.user
inp.email = request.user.email
# CODE -----------------------
name = inp.name
if name.endswith("S.L"):
inp.country = 'Company country'
inp.foundation_year = 'Company year'
.... And so on ...........
#CODE ! ----------------------
data = {'email': request.user.email, **Inputs.cleaned_data}
obtain_calculate_create_send(data)
inp.save()
messages.success(request, f'Your form is valid!')
return redirect('result/' + str(inp.pk) + '/')
else:
messages.warning(request, f'Please, check the inputs and enter valid ones')
content = {'Inputs': Inputs, 'title': 'valuations'}
else:
Inputs = InputsForm(request.POST or None)
content = {'Inputs': Inputs, 'title': 'valuations'}
return render(request, 'valuations/form_home.html', content)
嗨,问题是,如果表单提交了,这将正常工作,但我需要的是,用户显示的字段会根据输入动态更改,而不提交输入。例如,您填写了一个字段,然后出现了新字段,但没有提交,然后您提交了所有已填写的字段,其余字段在数据库中显示为空值或默认值。您好,问题是,如果表单已经提交,这将正常工作,但我需要的是,用户看到的字段会根据输入动态更改,而不提交输入。例如,您填写了一个字段,然后出现了新字段,但没有提交,然后提交了所有已填写的字段,其余字段在数据库中显示为空值或默认值。