如何将占位符添加到 Django-Registration 的表单中 [英] How to add placeholder to forms of Django-Registration

查看:22
本文介绍了如何将占位符添加到 Django-Registration 的表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用 django-registration.在我的 registration_form.html 文件中:

{{form.username}}{{form.email}}//其他领域

我想为每个字段设置占位符.但这是一种内置应用程序.所以我需要从我的主应用中找到编辑这些字段的方法.

我不想更改 django-registration 的来源.

解决方案

如果可以覆盖内置表单,则可以如下定义占位符:

class RegistrationForm(forms.ModelForm):元类:模型 = 你的模型名称小部件 = {'用户名' : forms.TextInput(attrs = {'placeholder': '用户名'}),'email' : forms.TextInput(attrs = {'placeholder': 'E-Mail'}),}

或者,您可以使用 jQuery 通过使用相应字段的 id 为字段添加占位符,如下所示:

<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}}');});

您可以使用firebug来查找字段的id.

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.

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'}),
        }

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>

You can use firebug to find the id of the field.

这篇关于如何将占位符添加到 Django-Registration 的表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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