0

I'm creating a website with a list of candidates where staff members can increment the grade of candidates by clicking in validate.

I would like to increment the value of grade when I click on validate in the modal. It works because I can see in the admin panel that the grade of the candidate has been incremented but it does not redirect me in the list of candidates that was before.

How can I do to redirect to the list of candidates that was before ?

Here is the template displaying the list of candidates and the script displaying the modal :

<table id="students-table" class="table">
<thead>

</thead>
<tbody>
    {% for student in student_list %}
    <tr>
        <td>
            <button type="button" class="validate-student bs-modal btn btn-sm btn-primary"
                data-form-url="{% url 'students:validate_grade' student.pk %}">
                <span class="fa fa-eye"></span>
            </button>
        </td>
    </tr>
    {% endfor %}
</tbody>
<script type="text/javascript">
$(function () {

    $(".validate-student").each(function () {
        $(this).modalForm({ formURL: $(this).data("form-url")});
    });
});

Here is the modal :

{% load widget_tweaks %}

<form method="POST" action="">
{% csrf_token %}

<div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel"><strong>{{ student.user.firstname }} {{ student.user.lastname
            }}</strong></h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>

<div class="modal-body">
    <p>Are you sure to validate this candidate ? ?
    </p>
</div>

<div class="modal-footer">
    <button type="submit" class="btn btn-success">Validate</button>
</div>

Here is the view :

def validate_grade(request, pk):
    context ={}
    obj = get_object_or_404(Student, id = pk)

    if request.method =="POST":
        obj.grade_registration += 1
        obj.save()
        return HttpResponseRedirect(reverse('list_student', args=[1945]))

    return render(request, "students/validate_student.html", context)
anthonya
  • 565
  • 2
  • 6
  • 15
  • What do you mean by "redirect me in the list of candidates that was before"? What are you currently redirected back to? – Iain Shelvington Oct 16 '21 at 20:41
  • I mean come back to the list of candidates. In the process, I have a list of candidates and each candidate has a validate button. Then I click on the button and it shows me the modal confirmation form. Then I have to click on validate to confirm the validation. Then the candidate is validated in the higher grade and the page is refreshing with the new list of candidates without the candidate that has been validated. – anthonya Oct 16 '21 at 20:53
  • I think you need to have your modal form post back using ajax. That way you don't need to handle a full refresh. You'll need to update the page from javascript after a valid response from validate_grade(). This makes for a smoother interface. – bmiller Oct 16 '21 at 21:04
  • I don't know ajax, can you send me a link to make this function ? – anthonya Oct 16 '21 at 21:13
  • Well here's one; https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications – bmiller Oct 16 '21 at 21:44

1 Answers1

0

you have to render the page again after the vote

def validate_grade(request, pk):
    context ={}
    obj = get_object_or_404(Student, id = pk)

    if request.method =="POST":
        obj.grade_registration += 1
        obj.save()

    return redirect('list_of_candidates')

You must redirect to the original page, that will be loaded with the updated database. Sso modify this last line:

    return render(request, "students/validate_student.html", context)
Paolo Vezzola
  • 105
  • 1
  • 5