如何在Django注册表单中添加占位符 [英] How to add placeholder to forms of Django-Registration
问题描述
我正在为项目使用django-registration。在我的 registration_form.html
文件中:
I am using django-registration for my project. in my registration_form.html
file:
{{form.username}}
{{form.email}}
//other fields
我想为每个字段设置占位符。但这是一种内置应用程序。因此我需要从主应用程序中找到编辑这些字段的方法。
And I want to set placeholders for each field. But this is a kind of built-in app. so I need to find the way for editing these fields from my main app.
我不想更改django注册的来源。
I don't want to change source of django-registration.
推荐答案
如果您可以覆盖内置表单,则可以按以下方式定义占位符:
If you can override the built-in form, you can define the placeholder as follows:
class RegistrationForm(forms.ModelForm):
class Meta:
model = YourModelName
widgets = {
'username' : forms.TextInput(attrs = {'placeholder': 'Username'}),
'email' : forms.TextInput(attrs = {'placeholder': 'E-Mail'}),
}
否则,您可以使用jQuery通过使用相应字段的 id向字段添加占位符
如下所示:
Or else you can use jQuery to add placeholder to the fields by using the corresponding field's id
as given below:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#id_username").attr('placeholder', '{{form.username.label}}');
$("#id_email").attr('placeholder', '{{form.email.label}}');
});
</script>
您可以使用萤火虫查找该字段的 id
。
You can use firebug to find the id
of the field.
这篇关于如何在Django注册表单中添加占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!