Skip to main content

Django Method Override

Django middleware that overrides the HTTP method through either a _method form param (Ruby on Rails style) or the X-HTTP-Method-Override header.

Installation

  1. Install via pip:

    pip install django-method-override
    
  2. Add the MethodOverrideMiddleware after Django's CsrfViewMiddleware:

    MIDDLEWARE = (
        # ...
        'django.middleware.csrf.CsrfViewMiddleware',
        'method_override.middleware.MethodOverrideMiddleware',
        # ...
    )
    
  3. Add method_override to your INSTALLED_APPS:

    INSTALLED_APPS = (
        # ...
        'method_override',
    )
    

Usage

Use the provided template tag in your form to add the desired form:

{% load method_override %}
<form action="{% url 'post-detail' %}" method="POST">
    {% csrf_token %}
    {% method_override 'PUT' %}
</form>

Now, you may use put in you Class-based views. Django Method Override will even copy over the form data to request.PUT:

class PostView(View):
    def put(self, request):
        form = Form(request.PUT)
        # ...

The X-HTTP-Method-Override header is also supported. So for the above view, this will work too:

$.ajax({
    headers: { "X-HTTP-Method-Override": "PUT" },
    type: "POST",
    url: "http://localhost:8000/posts/1/"
    // ...
});

Configuration

Django Method Override can be customized from your Django settings.py file:

METHOD_OVERRIDE_ALLOWED_HTTP_METHODS

A list of the allowed methods for overriding. Defaults to:

['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'PATCH']

METHOD_OVERRIDE_PARAM_KEY

The form param key used to override the method. Defaults to '_method'.

METHOD_OVERRIDE_HTTP_HEADER

The HTTP header to check. Defaults to 'HTTP_X_HTTP_METHOD_OVERRIDE'

METHOD_OVERRIDE_INPUT_TEMPLATE

The string tempalte used to render the input for the form param. It will based to kwargs, name and value. Defaults to:

'<input type="hidden" name="{name}" value="{value}">'

Copyright (c) 2013 LocalMed, Inc.. See LICENSE for details.

Release files for django-method-override 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-method-override 1.0.4
File Size Uploaded
django-method-override-1.0.4.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-method-override 1.0.4
File Interpreter ABI Platform
django_method_override-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / django-method-override-1.0.4.tar.gz

Download URL django-method-override-1.0.4.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e8c9b6d4f5c565b63703f6619d89a2c5e6d5b1b2e7304372c275d779c8934fea
BLAKE2b-256 checksum
How to use checksums
bc63ff0cb908aefce5b0dcf122c435d4a9bd55fe15745e7faf663e7a407395ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.2

Release files / django_method_override-1.0.4-py3-none-any.whl

Download URL django_method_override-1.0.4-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0df5f7b84b8c41a8c6dc2e487a9e5782aa969dff0991fa8d9ba61f437fbf66f4
BLAKE2b-256 checksum
How to use checksums
0cdcfe63aa3cd22217c00b004953cb8049d1d0879611b7fac3638b0229b981c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page