mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-31 07:57:55 +03:00 
			
		
		
		
	Made templates compatible with session-based CSRF. (#6207)
This commit is contained in:
		
							parent
							
								
									1660469ed8
								
							
						
					
					
						commit
						eb3180173e
					
				|  | @ -38,7 +38,7 @@ function sameOrigin(url) { | ||||||
|     !(/^(\/\/|http:|https:).*/.test(url)); |     !(/^(\/\/|http:|https:).*/.test(url)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var csrftoken = getCookie(window.drf.csrfCookieName); | var csrftoken = window.drf.csrfToken; | ||||||
| 
 | 
 | ||||||
| $.ajaxSetup({ | $.ajaxSetup({ | ||||||
|   beforeSend: function(xhr, settings) { |   beforeSend: function(xhr, settings) { | ||||||
|  |  | ||||||
|  | @ -247,7 +247,7 @@ | ||||||
|         <script> |         <script> | ||||||
|           window.drf = { |           window.drf = { | ||||||
|             csrfHeaderName: "{{ csrf_header_name|default:'X-CSRFToken' }}", |             csrfHeaderName: "{{ csrf_header_name|default:'X-CSRFToken' }}", | ||||||
|             csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}" |             csrfToken: "{{ csrf_token }}" | ||||||
|           }; |           }; | ||||||
|         </script> |         </script> | ||||||
|         <script src="{% static "rest_framework/js/jquery-3.3.1.min.js" %}"></script> |         <script src="{% static "rest_framework/js/jquery-3.3.1.min.js" %}"></script> | ||||||
|  |  | ||||||
|  | @ -290,7 +290,7 @@ | ||||||
|       <script> |       <script> | ||||||
|         window.drf = { |         window.drf = { | ||||||
|           csrfHeaderName: "{{ csrf_header_name|default:'X-CSRFToken' }}", |           csrfHeaderName: "{{ csrf_header_name|default:'X-CSRFToken' }}", | ||||||
|           csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}" |           csrfToken: "{% if request %}{{ csrf_token }}{% endif %}" | ||||||
|         }; |         }; | ||||||
|       </script> |       </script> | ||||||
|       <script src="{% static "rest_framework/js/jquery-3.3.1.min.js" %}"></script> |       <script src="{% static "rest_framework/js/jquery-3.3.1.min.js" %}"></script> | ||||||
|  |  | ||||||
|  | @ -1,7 +1,17 @@ | ||||||
|  | import re | ||||||
|  | 
 | ||||||
| from django.shortcuts import render | from django.shortcuts import render | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def test_base_template_with_context(): | ||||||
|  |     context = {'request': True, 'csrf_token': 'TOKEN'} | ||||||
|  |     result = render({}, 'rest_framework/base.html', context=context) | ||||||
|  |     assert re.search(r'\bcsrfToken: "TOKEN"', result.content.decode('utf-8')) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def test_base_template_with_no_context(): | def test_base_template_with_no_context(): | ||||||
|     # base.html should be renderable with no context, |     # base.html should be renderable with no context, | ||||||
|     # so it can be easily extended. |     # so it can be easily extended. | ||||||
|     render({}, 'rest_framework/base.html') |     result = render({}, 'rest_framework/base.html') | ||||||
|  |     # note that this response will not include a valid CSRF token | ||||||
|  |     assert re.search(r'\bcsrfToken: ""', result.content.decode('utf-8')) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user