이 답변 장고 등록을 할 특정 아니라.
django의 CsrfViewMiddleware는 csrf_token 컨텍스트 멤버에 액세스했다고 판단하면 csrf_token 쿠키를 설정합니다. 아쉽게도 Jinja2 렌더링은 Django의 미들웨어가 실행될 때까지 발생하지 않습니다. 결과적으로 쿠키가 설정되지 않으므로 양식과 일치하지 않으므로 403 오류가 발생합니다.
이 문제를 해결하려면 응답 처리를 완료하기 전에 어느 시점에서 컨텍스트 [ 'csrf_token']에 액세스해야합니다. 뷰 클래스에서 다음
class CsrfProtectMixin(object):
def render_to_response(self, context, **response_kwargs):
# Csrf processing happens when using a RequestContext.
# Be sure to use one.
if not isinstance(context, RequestContext):
context = RequestContext(self.request, context)
# Force access to csrf_token due to the way jinja2 renders the template
# after middleware has finished processing. Otherwise, the csrf cookie
# will not be set.
str(context.get('csrf_token'))
return super(CsrfProtectMixin, self).render_to_response(context, **response_kwargs)
: 그리고 당신은 클래스 기반 뷰를 사용하는 경우
, 당신이 CsrfProtectMixin를 만들 수 있습니다
class MyView(CsrfProtectMixin, TemplateView):
def get(self, request, *args, **kwargs):
context = {}
return self.render_to_response(context)
당신이 사용하지 않는 경우 클래스 기반 클래스의 논리
def my_view(request):
context = RequestContext(request)
str(context['csrf_token']) #force access to the csrf_token
return render_to_response('template.html', context)
아니면 원숭이 패치 render_to_reponse :보기, 당신은 뭔가를 할 수 있습니다 위.
서식 파일의 모양은 무엇입니까? – JonC
{% 연장 "site_base.html"%} {% 블록 본체 %}
{%의 엔드 블록 신체 % <입력 유형 = 값 = "제출"/ "제출">을 – mgPePe