如何自定义Django ModelForm字段? [英] How to customize Django ModelForm fields?

查看:58
本文介绍了如何自定义Django ModelForm字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django中自定义我的ModelForm.表单如下:

Hi I am trying to customize my ModelForm in Django. The form looks like this:

class RegistrationForm(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['name', 'age', 'details'......]

我试图将不同的类以及占位符和无标签一起添加到不同的字段.我怎么做?有没有办法在模板中执行此操作?

I am trying to add different classes to different fields, along with placeholders and no labels. How do I do that? Is there a way to carry this out in the templates?

推荐答案

您可以使用小部件将类和其他参数添加到字段中

You can add class and other parameters to a field using widget

   class RegistrationForm(forms.ModelForm):
      name = forms.CharField(label=' ',widget=forms.TextInput(attrs={'class':'textClass','placeholder':'Enter Name'}))
      details = forms.CharField(widget=forms.Textarea(attrs={'class':'special','maxlength':'100'}))
      class Meta:
         model = Registration
         fields = ['name', 'age', 'details'......]

简要说明:文档

这篇关于如何自定义Django ModelForm字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆