Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Widget form trong Django


Widget kiểm soát cách hiển thị trường form (input, textarea, select...). Tùy chỉnh qua tham số widget=forms.Textarea(attrs={...}).

Ví dụ

class CommentForm(forms.Form):
    comment = forms.CharField(
        widget=forms.Textarea(attrs={'class': 'comment', 'title': 'add comment'})
    )

Kết quả

Trường comment render thành <textarea class="comment" title="add comment"> thay vì <input> mặc định.