'How would you add a crsf token inside the html

form="<form action='addAppointment/' method='POST' enctype='multipart/form-data'><button type='submit'>Book now</button></form>"

I am currently adding the form element to a page through html the goal of this is to book an appointment.

How would I go about adding the crsf_token into the string.

{% csrf_token %}

Traceback

Forbidden (403)
CSRF verification failed. Request aborted.

Help
Reason given for failure:

    CSRF token missing or incorrect.

Got it to work like so

token = django.middleware.csrf.get_token(request)
print("token: ",token)
form='<form action="/addAppointment/'+self.username+"/"+time+ '/"'+ ' method="POST" enctype="multipart/form-data"><button type="submit">Book now</button>'+'<input name="csrfmiddlewaretoken"'+'value="'+token+'"'+ 'type="hidden">'+'</form>'
                    


Solution 1:[1]

In form {% csrf_token %} has to be inside the form. The best just after opening it.

<form method="post" ...>
    {% csrf_token %}
    # rest of the form
</form>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NixonSparrow