Skip to content

Commit 4f16c54

Browse files
committed
Merge pull request #4049 from clintonb/csrf-cookie-fix
Added support for custom CSRF cookie names
2 parents 1823662 + 9d9658f commit 4f16c54

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

rest_framework/renderers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from collections import OrderedDict
1313

1414
from django import forms
15+
from django.conf import settings
1516
from django.core.exceptions import ImproperlyConfigured
1617
from django.core.paginator import Page
1718
from django.http.multipartparser import parse_header
@@ -657,7 +658,8 @@ def get_context(self, data, accepted_media_type, renderer_context):
657658

658659
'display_edit_forms': bool(response.status_code != 403),
659660

660-
'api_settings': api_settings
661+
'api_settings': api_settings,
662+
'csrf_cookie_name': settings.CSRF_COOKIE_NAME,
661663
}
662664
return context
663665

rest_framework/static/rest_framework/js/csrf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function sameOrigin(url) {
3333
!(/^(\/\/|http:|https:).*/.test(url));
3434
}
3535

36-
var csrftoken = getCookie('csrftoken');
36+
var csrftoken = getCookie(window.drf.csrfCookieName);
3737

3838
$.ajaxSetup({
3939
beforeSend: function(xhr, settings) {

rest_framework/templates/rest_framework/admin.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ <h4 class="modal-title" id="myModalLabel">{{ error_title }}</h4>
230230
{% if filter_form %}{{ filter_form }}{% endif %}
231231

232232
{% block script %}
233+
<script>
234+
window.drf = {
235+
csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}"
236+
};
237+
</script>
233238
<script src="{% static "rest_framework/js/jquery-1.11.3.min.js" %}"></script>
234239
<script src="{% static "rest_framework/js/ajax-form.js" %}"></script>
235240
<script src="{% static "rest_framework/js/csrf.js" %}"></script>

rest_framework/templates/rest_framework/base.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ <h1>{{ name }}</h1>
258258
</div><!-- ./wrapper -->
259259

260260
{% block script %}
261+
<script>
262+
window.drf = {
263+
csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}"
264+
};
265+
</script>
261266
<script src="{% static "rest_framework/js/jquery-1.11.3.min.js" %}"></script>
262267
<script src="{% static "rest_framework/js/ajax-form.js" %}"></script>
263268
<script src="{% static "rest_framework/js/csrf.js" %}"></script>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy