简体   繁体   English

如何在 Django 的 CharField 上添加占位符?

[英]How do I add a placeholder on a CharField in Django?

Take this very simple form for example:以这个非常简单的表格为例:

class SearchForm(Form):
    q = forms.CharField(label='search')

This gets rendered in the template:这在模板中呈现:

<input type="text" name="q" id="id_q" />

However, I want to add the placeholder attribute to this field with a value of Search so that the HTML would look something like:但是,我想将placeholder属性添加到该字段,其值为Search以便 HTML 如下所示:

<input type="text" name="q" id="id_q" placeholder="Search" />

Preferably I would like to pass the placeholder value in to CharField in the form class through a dictionary or something like:最好我想通过字典或类似的东西将占位符值传递给表单类中的CharField

q = forms.CharField(label='search', placeholder='Search')

What would be the best way to accomplish this?实现这一目标的最佳方法是什么?

Look at the widgets documentation .查看小部件文档 Basically it would look like:基本上它看起来像:

q = forms.CharField(label='search', 
                    widget=forms.TextInput(attrs={'placeholder': 'Search'}))

More writing, yes, but the separation allows for better abstraction of more complicated cases.更多的写作,是的,但分离允许更好地抽象更复杂的情况。

You can also declare a widgets attribute containing a <field name> => <widget instance> mapping directly on the Meta of your ModelForm sub-class.您还可以直接在ModelForm子类的Meta上声明一个包含<field name> => <widget instance>映射的widgets属性。

For a ModelForm, you can use the Meta class thus:对于 ModelForm,您可以这样使用 Meta 类:

from django import forms

from .models import MyModel

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
            'name': forms.TextInput(attrs={'placeholder': 'Name'}),
            'description': forms.Textarea(
                attrs={'placeholder': 'Enter description here'}),
        }

The other methods are all good.其他方法都不错。 However, if you prefer to not specify the field (eg for some dynamic method), you can use this:但是,如果您不想指定字段(例如,对于某些动态方法),您可以使用:

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    self.fields['email'].widget.attrs['placeholder'] = self.fields['email'].label or 'email@address.nl'

It also allows the placeholder to depend on the instance for ModelForms with instance specified.它还允许占位符依赖于具有指定实例的 ModelForms 的实例。

You can use this code to add placeholder attr for every TextInput field in you form.您可以使用此代码为表单中的每个 TextInput 字段添加占位符 attr。 Text for placeholders will be taken from model field labels.占位符的文本将从模型字段标签中获取。

class PlaceholderDemoForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(PlaceholderDemoForm, self).__init__(*args, **kwargs)
        for field_name in self.fields:
            field = self.fields.get(field_name)  
            if field:
                if type(field.widget) in (forms.TextInput, forms.DateInput):
                    field.widget = forms.TextInput(attrs={'placeholder': field.label})

    class Meta:
        model = DemoModel

Great question.很好的问题。 There are three solutions I know about:我知道三种解决方案:

Solution #1解决方案#1

Replace the default widget.替换默认小部件。

class SearchForm(forms.Form):  
    q = forms.CharField(
            label='Search',
            widget=forms.TextInput(attrs={'placeholder': 'Search'})
        )

Solution #2解决方案#2

Customize the default widget.自定义默认小部件。 If you're using the same widget that the field usually uses then you can simply customize that one instead of instantiating an entirely new one.如果您使用该字段通常使用的相同小部件,那么您可以简单地自定义该小部件,而不是实例化一个全新的小部件。

class SearchForm(forms.Form):  
    q = forms.CharField(label='Search')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['q'].widget.attrs.update({'placeholder': 'Search'})

Solution #3解决方案#3

Finally, if you're working with a model form then ( in addition to the previous two solutions ) you have the option to specify a custom widget for a field by setting the widgets attribute of the inner Meta class.最后,如果您正在使用模型表单,那么(除了前两个解决方案)您可以选择通过设置内部Meta类的widgets属性来为字段指定自定义小widgets

class CommentForm(forms.ModelForm):  
    class Meta:
        model = Comment
        widgets = {
            'body': forms.Textarea(attrs={'cols': 80, 'rows': 20})
        }

Most of the time I just wish to have all placeholders equal to the verbose name of the field defined in my models大多数时候,我只是希望所有占位符都等于我的模型中定义的字段的详细名称

I've added a mixin to easily do this to any form that I create,我添加了一个 mixin 来轻松地对我创建的任何表单执行此操作,

class ProductForm(PlaceholderMixin, ModelForm):
    class Meta:
        model = Product
        fields = ('name', 'description', 'location', 'store')

And

class PlaceholderMixin:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        field_names = [field_name for field_name, _ in self.fields.items()]
        for field_name in field_names:
            field = self.fields.get(field_name)
            field.widget.attrs.update({'placeholder': field.label})

It's undesirable to have to know how to instantiate a widget when you just want to override its placeholder.当您只想覆盖其占位符时,必须知道如何实例化小部件是不可取的。

    q = forms.CharField(label='search')
    ...
    q.widget.attrs['placeholder'] = "Search"
class FormClass(forms.ModelForm):
    class Meta:
        model = Book
        fields = '__all__'
        widgets = {
            'field_name': forms.TextInput(attrs={'placeholder': 'Type placeholder text here..'}),
        }

After looking at your method, I used this method to solve it.看了你的方法,我用这个方法解决了。

class Register(forms.Form):
    username = forms.CharField(label='用户名', max_length=32)
    email = forms.EmailField(label='邮箱', max_length=64)
    password = forms.CharField(label="密码", min_length=6, max_length=16)
    captcha = forms.CharField(label="验证码", max_length=4)

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    for field_name in self.fields:
        field = self.fields.get(field_name)
        self.fields[field_name].widget.attrs.update({
            "placeholder": field.label,
            'class': "input-control"
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM