POST

POST request in Django

POST requests are commonly used to send form data from the browser to the server.
In Django, submitted form values can be accessed using request.POST.

 

Template example

<form method="POST">
    {% csrf_token %}
    <input type="text" name="param1">
    <input type="text" name="param2">
    <input type="submit" value="Submit">
</form>


Retrieving POST data in the backend

class index(TemplateView):
    template_name = 'index.html'

    def post(self, request, *args, **kwargs):

        param1 = request.POST.get('param1', 'default_value1')
        param2 = request.POST.get('param2', 'default_value2')

        print(param1)
        print(param2)

        context = self.get_context_data(**kwargs)
        context['param1'] = param1
        context['param2'] = param2

        return self.render_to_response(context)

Tested with Django 4.2