Kintarō
Kintarō

Reputation: 3187

How to use Django CSRF token correctly?

I followed the django doc about how to use the CSRF token. (https://docs.djangoproject.com/en/dev/ref/contrib/csrf/)

I understand the first two steps but I got confused on the step 3. In step 3, there are two options.

Option 1: UseRequestContext.

Option 2: Manually generate the CSRF token and add it to the template context.

If I want to use Option 1 and implement a view class from the base view class, do I need to do extra to generate the token?

Thanks.

Upvotes: 0

Views: 8714

Answers (1)

catherine
catherine

Reputation: 22808

settings

MIDDLEWARE_CLASSES = [
    //other middlewares

    'django.middleware.csrf.CsrfViewMiddleware',

]

template

<form method="post">
    {% csrf_token %}
    ......
</form>

views

def view_name(request):
    if request.method == 'POST':
        .....

Upvotes: 9

Related Questions