2012-02-12 4 views
7

views.py를 인쇄하지 csrf_token !장고 숨겨진 입력 필드

<html> 
<body> 
<h1> All uploaded posters: </h1> 
<form action='/posters/upload' method= 'POST'> 
<input type='file' name= 'uploadfile'>Upload new poster <input type="submit" value = "Upload"> 
</form> 

<a href= ...... 

내가 무엇을 놓쳤을까요?

UPDATE : this을 도왔다.

답변

8

당신은 CSRF 미들웨어를 사용하기 위해 RequestContext를 사용할 필요가 :

from django.template import RequestContext 

# In your view: 
return render_to_response('index.html' 
    {'files':os.listdir('/home/username/public_html/posters') }, 
    context_instance=RequestContext(request)) 

BTW : 당신이 그것을 사용하는 것을 잊지 경우, 당신은 보안 구멍이 때문에 csrf_protect 장식의 사용이 권장되지 않습니다.

from django.shortcuts import render 

def some_view(request): 
    return render(request, 'template.html', context_dict) 
+0

감사합니다. 다행 스러웠습니다. – Cerin

1

는 1.3에 일단 (당신이해야하는)에 render 바로 그 일을보다 컴팩트 한 방법을 제공합니다.

데코레이터 방법 담요 보호로 CsrfViewMiddleware를 추가하는 대신 보호가 필요한 특정보기에 정확히 동일한 기능을 가진 csrf_protect 데코레이터를 사용할 수 있습니다. 출력에 CSRF 토큰을 삽입하는보기와 POST 양식 데이터를 승인하는보기 모두에서 사용해야합니다. (이들은 종종 같은보기 기능이지만 항상 그런 것은 아닙니다). 그것은 다음과 같이 사용됩니다 : 당신이 그것을 사용하는 것을 잊지 경우, 당신은 보안 구멍이 때문에 데코레이터의

from django.views.decorators.csrf import csrf_protect 
from django.template import RequestContext 

@csrf_protect 
def my_view(request): 
    c = {} 
    # ... 
    return render_to_response("a_template.html", c, 
           context_instance=RequestContext(request)) 

사용은 그 자체 권장되지 않습니다. 두 가지를 모두 사용하는 '벨트 및 브레이스'전략은 좋으며 최소한의 오버 헤드가 발생합니다.

0

장고 문서의 미리보기를 참조하십시오