2009-12-22 5 views
0

render_to_response를 사용하여 반환 된 html이 이스케이프되어야합니다. 적합한 문서를 찾을 수 없습니다. 누군가가 어떤 방향으로 향하게 할 수 있습니까?어떻게 장고에서 HttpResponse 개체를 수정합니까?

코드는 다음과 같습니다

여기
return render_to_response(template_name, {return render_to_response(template_name, { 
      'form': form, 
      redirect_field_name: redirect_to, 
      'site': current_site, 
      'site_name': current_site.name, 
     }, context_instance=RequestContext(request)) 

나는 탈출 할 수있는 응답 HTML 텍스트가 필요합니다. 내가 아는 한 방법은 문자열에서 템플릿 파일을 읽고 re.escape()로 이스케이프 처리 한 다음 렌더링하는 것입니다. 뭐하는 청소기와 간단한 방법 뭐죠 ??

답변

2

render_to_response을 통해 뷰에서 템플릿으로 전달 된 문자열은 기본적으로 이스케이프됩니다.

은 참조 : 장고 기본적으로 http://docs.djangoproject.com/en/dev/topics/templates/#id2

가 모든 템플릿이 자동으로 모든 변수 태그의 출력을 이스케이프합니다. 특히,이 다섯 개 문자는 이스케이프 :

< is converted to &lt; 
> is converted to &gt; 
' (single quote) is converted to &#39; 
" (double quote) is converted to &quot; 
& is converted to &amp; 

는 다시, 우리는이 문제는 기본적으로 설정되어 있음을 강조한다.

2

전체 응답을 이스케이프 처리하는 것처럼 들리겠습니까? 사실입니까? 조금 이상한 것 같지만 확실히 할 수 있습니다.

import django.utils.html as html 
string = render_to_string(<all of the args to render_to_response>) 
escaped_string = html.escape(string) 
return HttpResponse(escaped_string) 

나는 다른 쪽 끝에있는 사람/브라우저 이와을 어떻게 할 것인지 확실히 모르겠지만, 당신이 요구 한 것 같은 소리.

관련 문제