I tried to get multiple data from the checkbox using method below in my views.py:
if (((request.POST.get('data_choice ')) == ('Salary')) and ((request.POST.get('data_choice ')) and ((request.POST.get('data_choice ')) == ('Overtime')) ):
But the problem is the "and" condition is not working but if i use "or" condition it's work. May I know what's the problem with my code? Is the request.POST.get can only get 1 data?
choices.py
class DataChoices(models.TextChoices):
salary = 'Salary',_('Salary')
bonus = 'Bonus',_('Bonus')
allowance= 'Allowance',_('Allowance')
overtime= 'Overtime',_('Overtime')
models.py
class Data(models.Model):
Data_choices = Data_choices
data_choices = models.CharField(max_length=40,choices=Data_choices.choices,blank = True, null= True)
forms.py
class dataForm(forms.ModelForm):
datachoice = DataChoices
data_choice = forms.MultipleChoiceField(label=False,choices=datachoice .choices, required=True,widget=forms.CheckboxSelectMultiple(
attrs= {
"checked":"checked"
}))
class Meta:
model = Data
fields = ('__all__')
html
<form class="form" method="POST">
{% csrf_token %}
<fieldset class="form-group">
<h4 style="display:inline-block;" class="title-section">PRINT FORM</h4>
<div class="card">
<div class="card-body">
<div class="row col" >
<h5>Please select data to be included in the form</h5>
</div>
<div class="row mt-4">
<div class="col-lg">{{ form.data_choice |as_crispy_field }}</div>
</div>
</div>
<div class="col " style="text-align:center;">
<button class='btn btn-info btn-block'>Next</button>
</div>
</div>
</fieldset>
</form>
views.py
if form.is_valid:
data = {
'data_choice ':request.POST.get('data_choice '),
}
if (((request.POST.get('data_choice ')) == ('Salary')) and ((request.POST.get('data_choice ')) == ('Bonus')) ):
request.session['decision_data'] = data
return redirect ('printformA')
else:
messages.error(request, f'Error in creating Form')
return render(request, 'selectdata.html',context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.