Skip to main content

Jinja2 extension to render csrf hidden input

Project description

Jinja2 CSRF Token Extension

This is a Jinja2 extension for generating CSRF tokens in Django templates.

Installation

Install the extension using pip:

pip install jinja2_csrf

Configuration

To use this extension, you need to configure Jinja2 as a template engine in your Django settings:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'environment': 'path.to.your.jinja2.environment',
            'extensions': ['jinja2_csrf.CSRFTokenExtension'],
        },
    },
]

Make sure to replace 'path.to.your.jinja2.environment' with the actual path to your Jinja2 environment.

Usage

In your Jinja2 templates, you can now use the csrf_token tag to generate a CSRF token:

<form method="post">
    {% csrf_token %}
    <!-- Your form fields go here -->
    <button type="submit">Submit</button>
</form>

The csrf_token tag will generate an <input> element with the CSRF token value:

<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

Make sure to include the csrf_token tag within your form to protect against CSRF attacks.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2_csrf-1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

jinja2_csrf-1.0-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jinja2_csrf-1.0.tar.gz.

File metadata

  • Download URL: jinja2_csrf-1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for jinja2_csrf-1.0.tar.gz
Algorithm Hash digest
SHA256 4c3d1f4c87c808b417b9010e57ddfc7a9e40b285e6361957d3dca1e24d6071f0
MD5 b765a29367c2da94a51aaed7a7cc00ef
BLAKE2b-256 afd3474af8fff74ba26c3559ab65a725dee7f40d213e5bf07bc3d932df30c815

See more details on using hashes here.

File details

Details for the file jinja2_csrf-1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: jinja2_csrf-1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for jinja2_csrf-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5de440dd5190b0e038e5020d84ab07981f35f00a2489854cbef22471982c177d
MD5 f0f1034a631aa694f178f20942febbe0
BLAKE2b-256 0ed01aa3c4d4498491b36cca9a2b8993d27e91d399e04bdd1ede9f3193cb902d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page