Opened 4 weeks ago

Last modified 3 weeks ago

#37032 assigned Cleanup/optimization

Clarify that django.template.context_processors.csrf is enabled by default in ref docs

Reported by: Christian Finnberg Owned by: VIZZARD-X
Component: Documentation Version: dev
Severity: Normal Keywords: csrf, context processor
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Documentation for django.template.context_processors.csrf in https://docs.djangoproject.com/en/dev/ref/templates/api/#django-template-context-processors-csrf is not accurate. It says:

This processor adds a token that is needed by the csrf_token template tag for protection against Cross Site Request Forgeries.

But the csrf_token template tag is independent of this context processor. This content processor adds a csrf_token variable that can be used for protection against CSRF, but it seems that this is like a "legacy" method. The recommended way or at least the way Django is configured by default, is to enable the django.middleware.csrf.CsrfViewMiddleware middleware, that adds the crsf_token template tag and works independently of this context processor.

So a different definition may be better. Something like:

If this processor is enabled, every RequestContext will contain a variable csrf_token with a CSRF token, or the string 'NOTPROVIDED' if it has not been provided by either a view decorator or the middleware. Notice that the csrf_token template tag (not this context processor) is the preferred way to add the CSRF token to the forms.

In any case I think this component's text must be corrected somehow.

Change History (6)

comment:1 by Sarah Boyce, 4 weeks ago

Resolution: invalid
Status: newclosed

The docs are correct, the template tag gets the csrf_token which is added by the context processor. However, this context processor is enabled by default regardless of your settings. See https://docs.djangoproject.com/en/6.0/ref/templates/api/#:~:text=In%20addition%20to%20these,option

in reply to:  1 comment:2 by Christian Finnberg, 4 weeks ago

Replying to Sarah Boyce:

The docs are correct, the template tag gets the csrf_token which is added by the context processor. However, this context processor is enabled by default regardless of your settings. See https://docs.djangoproject.com/en/6.0/ref/templates/api/#:~:text=In%20addition%20to%20these,option

Thanks for the correction. I didn't noticed your highlighted paragraph. Sorry for the noise then.
In any case I still think it would be helpful for the developer reading the documentation to get this information also from the django.template.context_processors.csrf part. At least I was reading the part of the documentation about context processors and I was confused about this one

comment:3 by Sarah Boyce, 4 weeks ago

Summary: Documentation for django.template.context_processors.csrf is incorrect or inaccurateClarify that django.template.context_processors.csrf is enabled by default in ref docs
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Sure, would you like to submit a patch?

comment:4 by Natalia Bidart, 4 weeks ago

Resolution: invalid
Status: closednew

Reopening so status gets updated from invalid to new.

comment:5 by VIZZARD-X, 4 weeks ago

Owner: set to VIZZARD-X
Status: newassigned

comment:6 by VIZZARD-X, 3 weeks ago

Has patch: set

Added a small note to make it clearer and more readable, along with a cross-reference to Using RequestContext for more info (seemed more in line with the Django way of doing things). This should suffice, let me know if anything else is needed.

Last edited 3 weeks ago by VIZZARD-X (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top