2013-12-21 4 views
0

는이 같은 뭔가가 필요 :django를 사용하여 입력 필드에 속성을 추가하는 방법은 무엇입니까?

<div> 
<input type="text"></input> 
<input type="hidden" name="attr1" value="true"> 
<input type="hidden" name="attr2" value="true"> 
<input type="hidden" name="attr3" value="false"> 
</div> 
<div> 
<input type="text"></input> 
<input type="hidden" name="attr11" value="true"> 
<input type="hidden" name="attr22" value="true"> 
<input type="hidden" name="attr33" value="true"> 
</div> 

내가 각 필드에 몇 가지 추가 비표준 속성을 연결해야합니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 위의 예제를 실현하는 방법? 나는 장고에서 사용자 정의 템플릿으로 이것을 할 수 있지만 불필요한 클라이언트 - 서버 통신입니다.

답변

2

양식에 원하는 속성을 설정할 수 있습니다.

class MyForm(forms.Form): 
    description = forms.Textarea(attrs={'my_attr': 'and_value'}) 

렌더링 된 결과는 다음과 같습니다 :

<textarea my_attr="and_value" name="description" id="id_description"></textarea> 
+0

좋아하는 예를 들어, 당신은이 같은 rowscols 속성을 설정할 수 있습니다. 하지만 나는 비표준 attrs를 부착해야합니다. myrows처럼 행이 아닙니다. – milandjukic88

+0

이런 식으로 모든 종류의 속성을 추가 할 수 있습니다. – niekas

+0

감사합니다. 그게 내가 필요한거야 :) – milandjukic88

관련 문제