2013-10-21 8 views
-2
내가 safe 필터를 사용하고

을 장고와 내가 <code></code> 태그 만 HTML 태그에 탈출 할, 그 안녕하세요하지만 <code><b>Hello</b></code><b>Hello</b>로 렌더링 될 것이다 <b>Hello</b> 렌더링됩니다 의미한다. 그래서 나는 사용자 정의 필터를 쓰기하지만 난이 오류 :사용자 정의 필터 AttributeError

Exception Type: AttributeError 
Exception Value:'ResultSet' object has no attribute 'replace' 
Exception Location: G:\python\Python\python practice\python website\firstpage\custom_filters\templatetags\custom_filters.py in code_escape, line 10 

내 코드는 다음과 같습니다

from bs4 import BeautifulSoup 

from django import template 
register = template.Library() 

@register.filter 
def code_escape(value): 
    soup = BeautifulSoup(value) 
    response = soup.find_all('code') 
    string = response.replace('<', '&lt;') 
    string = string.replace('>', '&gt;') 
    string = string.replace("'", '&#39') 
    string = string.replace('"', '&quot') 
    final_string = string.replace('&', '&amp') 
    return final_string 

template.html

....... 

{% load sanitizer %} 
{% load custom_filters %} 

...... 

{{ content|escape_html|safe|linebreaks|code_escape }} 

....... 
+0

"작동하지 않음"이란 의미를 설명 할 수 있습니까? 오류가 발생하면 여기에 넣을 수 있습니까? 몇 가지 샘플 데이터를 제공 할 수 있습니까? 그리고 그 데이터의 출력을 원하십니까? 이 코드 덤프가 정확히 우리가 찾고있는 곳입니까? –

+0

나는 내가 원하는 오류를 추가했다. –

답변

0

find_all는 반복자 인 ResultSet을 반환합니다. 그런 다음 .string 태그의 값을 수정할 수 있다고 생각합니다. 다음과 같이 시도해보십시오.

soup = BeautifulSoup(value) 
for code_tag in soup.find_all('code'): 
    code_tag.string = code_tag.string.replace('<', '&lt;') 
    code_tag.string = code_tag.string.replace('>', '&gt;') 
    code_tag.string = code_tag.string.replace("'", '&#39') 
    code_tag.string = code_tag.string.replace('"', '&quot') 
    code_tag.string = code_tag.string.replace('&', '&amp') 
return str(soup) 
+0

'.string'은 문자열 자체이기 때문에 'cod_tag'에 적용되지 않을 수 있습니다. 하지만 난 내 솔루션을 얻고 아래의 답변으로 퍼팅. 귀하의 노력에 감사드립니다 .... –