Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add placeholder to forms of Django-Registration

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.

like image 494
alioguzhan Avatar asked Nov 23 '12 04:11

alioguzhan


2 Answers

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.

like image 116
arulmr Avatar answered Sep 24 '22 01:09

arulmr


I would actually create a template tag filter that modifies the field.

@register.filter
def html_placeholder(field, args=None):
    if args == None:
        return field
    field.field.widget.attrs.update({ "placeholder": args })
    return field
like image 23
airtonix Avatar answered Sep 26 '22 01:09

airtonix