Mahad_Akbar
Mahad_Akbar

Reputation: 13

HTML button onclick action with Django function

HTML form on Django. I want to perform or create web page in which the button performs the action, and in backend I am handling the Django function.

The code of HTML file is:

<form name="bookappointment" class="form" method="POST">
   {% csrf_token %}

                  <br>
                  <input type="hidden", name="selecteddoctornumber" value="{{i.doctornumber}}">
<div class="row">

          <div class="col-md-1"></div>

          <div class="col-md-4">
              <button class="btn approve" name="approvebtn">Approved</button>

          </div>

          <div class="col-md-1"></div>

          <div class="col-md-4">
                  <button class="btn notapprove" name="notapprovebtn">Not Approved</button>
          </div>
               <br>
    </div>
                  <a class="btn cancel" href="requests">Cancel</a>

   <br>
   </form>

and the other side the Django function is:

if request.method == 'POST':

    if request.POST.get('approvebtn'):
        for i in doctor:
            if pendingDoctorNumber == i.contactNumber:
                i.status = 'Approved'

        return redirect('request')

    if request.POST.get('notapprovebtn'):
        for i in doctor:
            if pendingDoctorNumber == i.contactNumber:
                i.status = 'Not Approved'

        return redirect('request')

but its not working any action just get me back to same page

Upvotes: 1

Views: 1368

Answers (1)

sumeet
sumeet

Reputation: 51

<form action="{% url 'bookappointment' %}" method="POST">

you have to define bookappointment in your urls.py which redirect to views.py where your function lies with name bookappointment.

Upvotes: 1

Related Questions