Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 2 months ago
JSON representation
A drop-in React component for submitting forms with a Django CSRF middleware token.
- Host: GitHub
- URL: https://github.com/imerica/django-react-csrftoken
- Owner: iMerica
- License: mit
- Created: 2016-10-03T07:18:22.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-21T09:08:54.000Z (about 5 years ago)
- Last Synced: 2024-09-19T19:09:04.283Z (3 months ago)
- Topics: django, django-csrf-middleware, npm, npm-module, npm-package, react, reactjs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/django-react-csrftoken
- Size: 13.7 KB
- Stars: 30
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 (
// 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:
```pythonfrom django.views.decorators.csrf import ensure_csrf_cookie
@ensure_csrf_cookie
def register_view(request):
// ...```