0

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)

1 Answer 1

1

use QueryDict.getlist(key, default=None)

Usage:

data_choice = request.POST.getlist('data_choice')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.