pythondjangobuttonviewdetailview

DetailView + input button


I would like to add a button to "validate" a user towards the next step of his registration.

The staff member have to "validate" the user in the user detail page.

So I created a DetailView view with template with all the details of this user. But I would like to know if it is possible to write code in DetailView function to allow the user to increment his step_registration variable when I click on "validate" ?

Here is my DetailView function :

views.py

class StudentDetailView(DetailView, LoginRequiredMixin):
    model = Student
    login_url = login

student_detail.html

{% block content %}
  <h1>Etudiant: {{ student.user.firstname }} {{ student.user.firstname }}</h1>
  <ul>
    {% if student.photo %}
      <li> Photo :<img src="{{ student.photo.url }}", width = 250, height = 300>"> </li>
    {% endif %}
    <li> Etat de l'inscription : {{ student.step_registration }}</li>
    <li> Date de naissance : {{ student.birthdate }} </li>

  </ul>
<button class="btn btn-secondary"> Validate ! </button>

{% endblock %}

The step registration variable is called "step_registration", so I want to do student.step_registration += 1 when a staff member click on "validate"


Solution

  • To make the button only visible for staff members (you can also add logic to show it only at certain steps):

    {% if user.is_staff %}
    <button class="btn btn-secondary" href="/validate_student/student.id"> Validate ! </button>
    {% endif %}
    

    add the view and link it in urls.py (you maybe add some logic/checks or set the step to a certain value):

    from django.contrib.admin.views.decorators import staff_member_required
    
    @staff_member_required
    def validate_student(request, id):
      student = Student.objects.get(id)
      student.registration_step += 1
      student.save()
      return ... # redirect or success page