django-http-method

Provide a workaround to use different method from GET or POST inside HTML forms


Keywords
django, django2, django3, http, http-method, python, python3, rest
License
MIT
Install
pip install django-http-method==1.2.1

Documentation

Python package codecov CodeFactor PyPI Version Python 3.5+ Django 2+, 3+ License MIT

django-http-method

Provide a workaround to use different method than GET or POST inside HTML forms in django templates. Works only with Class Based View.

Installation

From source code:

python setup.py install

From pip:

pip install django-http-method

Usage

Add django_http_method to your settings.INSTALLED_APPS
INSTALLED_APPS = (
    [...],
    django_http_method,
    [...],
)
Add the mixin to a CBV
from django.views.generic import View
from django_http_method import HttpMethodMixin

class TestView(HttpMethodMixin, View):
	
	def get(self, request):
		pass
	
	def delete(self, request):
		pass
	
	def put(self, request):
		pass
	
	[...]
In your template, load http_method and use {% http_[method] %} in your forms:
{% load http_method %}

<form action="/" method="post">
    {% csrf_token %}
    {% http_put %}
    [...]
    <button type="submit">Send a PUT request</button>
</form>


<form action="/" method="post">
    {% csrf_token %}
    {% http_patch %}
    [...]
    <button type="submit">Send a PATCH request</button>
</form>

The corresponding method of your View will now be called. For instance, if {% http_put %} was used, then TestView.put() will be called and any request parameter will be in request.PUT.