[Fixed]-Set a Boolean when submitted in django

1๐Ÿ‘

If you want to set the boolean field to true, when the form is submitted, you just have to handle it in the view.

Submit the form and before saving it into the database just set the is_rep = true.
is_rep is a field which is in the model, but not used in the form.
So, if you want to change that then you have to manually write a view for it. Try to use base view instead of generic views to understand the workflow of the views and forms.

Iโ€™d recommend using something like this:

class RetestView(View):

    def get(self, request, *args, **kwargs):
        ..............
        return render(request, self.template, {"some_context"}

    def post(self, request, *args, **kwargs):
        form_data = your_form(request.POST)
        if form_data.is_valid():
            new_object = form_data.save(commit=False)
            new_object.is_rep = True
            new_object.save()
        return render(request, self.template, {"some...context"})

Hope you got what you were looking for..!

๐Ÿ‘คzaidfazil

0๐Ÿ‘

form_template

{% for field in form %}

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">{{ field.errors }} </span>
    </div>
    <label class="control-label col-sm-2">{{ field.label_tag }} </label>
    <div class ="col-sm-12">
    <div class="form-control">
    {{ field }}</div></div>
    </div>


{% endfor %}
๐Ÿ‘คnajmath

Leave a comment