The view has a Boolean Field which will define if a question is OK or needs correction. The template will load two buttons to act as submit to the form, “Question is OK” and “Question needs correction”. I need to pass the value of this button as the Boolean Field value. I found the answer when using Function-based views, but I’m using Class-based views, so I don’t know how to pass the request.POST values.
Here’s my views.py and forms.py:
views.py
JavaScript
x
24
24
1
class QuestionValidation(PermissionRequiredMixin, UpdateView):
2
permission_required = 'users.validator'
3
model = Question
4
form_class = ValidationForm
5
template_name = 'question_validation.html'
6
7
def get_context_data(self, *args, **kwargs):
8
context = super().get_context_data(**kwargs)
9
context['question'] = Question.objects.filter(
10
question_order=self.kwargs['order']).get(id_by_order=self.kwargs['id_by_order'])
11
context['order'] = self.kwargs['order']
12
context['id_by_order'] = self.kwargs['id_by_order']
13
return context
14
15
def get_object(self, *args, **kwargs):
16
question_order = Q(question_order__id=self.kwargs['order'])
17
question_id = Q(id_by_order__contains=self.kwargs['id_by_order'])
18
q = Question.objects.get(question_order & question_id)
19
return get_object_or_404(Question, pk=q.id)
20
21
def get_success_url(self, *args, **kwargs):
22
view_name = "order-detail"
23
return reverse(view_name, kwargs={'pk': self.kwargs['order']})
24
forms.py
JavaScript
1
9
1
class ValidationForm(forms.ModelForm):
2
class Meta:
3
model = Question
4
fields = ['revision_report', 'revision_approval']
5
widgets = {
6
'revision_report': forms.HiddenInput(),
7
'revision_approval': forms.HiddenInput(),
8
}
9
and part of the template that this code will be loaded:
JavaScript
1
9
1
<form action="" method="POST">{% csrf_token %}
2
{{ form.as_p }}
3
<button class="btn btn-success" name="question_approved">Questão aprovada</button>
4
<button class="btn btn-danger" name="question_refused">Questão não foi aprovada</button>
5
</form>
6
<br><br>
7
<script src="{% static 'js/hoverValidatorTextbox.js' %}"></script>
8
{% endblock %}
9
Advertisement
Answer
As Ene P told in the comments the following link solves it.