Python 如何在 Django 的 CharField 上添加占位符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4101258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a placeholder on a CharField in Django?
提问by Joelbitar
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 placeholderattribute to this field with a value of Searchso 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 CharFieldin 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?
实现这一目标的最佳方法是什么?
采纳答案by Mike Axiak
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 widgetsattribute containing a <field name> => <widget instance>mapping directly on the Metaof your ModelFormsub-class.
您也可以声明widgets包含一个属性<field name> => <widget instance>直接映射Meta你的ModelForm子类。
回答by Yevgeniy Shchemelev
You can use this code to add placeholder attr for every TextInput field in you form. Text for placeholders will be taken from model field labels.
您可以使用此代码为表单中的每个 TextInput 字段添加占位符 attr。占位符的文本将从模型字段标签中获取。
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
回答by Hamish Downer
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'}),
}
回答by Mark
The other methods are all good. However, if you prefer to not specify the field (e.g. 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 protected]'
It also allows the placeholder to depend on the instance for ModelForms with instance specified.
它还允许占位符依赖于具有指定实例的 ModelForms 的实例。
回答by Dwayne Crooks
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 widgetsattribute of the inner Metaclass.
最后,如果您正在使用模型表单,那么(除了前两个解决方案)您可以选择通过设置widgets内部Meta类的属性来为字段指定自定义小部件。
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
widgets = {
'body': forms.Textarea(attrs={'cols': 80, 'rows': 20})
}
回答by cdosborn
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"
回答by Viktor Johansson
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})
回答by lber l
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"
})

