Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/imerica/django-react-csrftoken

A drop-in React component for submitting forms with a Django CSRF middleware token.
https://github.com/imerica/django-react-csrftoken

django django-csrf-middleware npm npm-module npm-package react reactjs

Last synced: 22 days ago
JSON representation

A drop-in React component for submitting forms with a Django CSRF middleware token.

Awesome Lists containing this project

README

        

# DjangoCSRFToken

![https://circleci.com/gh/iMerica/django-react-csrftoken/](https://circleci.com/gh/iMerica/django-react-csrftoken.png?style=shield)

A drop-in React component for submitting forms with a Django CSRF middleware token.

## Installation

npm install --save django-react-csrftoken

## Usage

```javascript

import React from 'react';
import DjangoCSRFToken from 'django-react-csrftoken'

class MyLoginForm extends React.Component {
render(){
return (




// email
// password
// submit button


)
}
}
```

## CSRF Cookie and React

Because react renders elements dynamically, Django might not set a CSRF token cookie if you render a form using react.
This is described in [the Django docs](https://docs.djangoproject.com/en/1.11/ref/csrf/):
> If your view is not rendering a template containing the csrf_token template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie: ensure_csrf_cookie().

To fix this problem add the decorator mentioned above to your views:
```python

from django.views.decorators.csrf import ensure_csrf_cookie

@ensure_csrf_cookie
def register_view(request):
// ...

```