How to add placeholder UserCreationForm django?











up vote
0
down vote

favorite












I'm trying to add a placeholder to the username field in the UserCreationForm
but i can't understand how to do it.
I have already change the forms.py file in this way /lib/site-packages/django/contrib/auth/forms.py.



I add a placeholder to password1 and password2 fields and work like this:



class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(attrs={'placeholder':'Password'}),
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput(attrs={'placeholder':'Confirmar Password'}),
strip=False,
help_text=_("Enter the same password as before, for verification."),
)


I can see probably the username field is coming from class meta:



class Meta:
model = User
fields = ("username",)
field_classes = {'username': UsernameField}


from this class but i'm not sure about that



class UsernameField(forms.CharField):
def to_python(self, value):
return unicodedata.normalize('NFKC', super().to_python(value))


I don't understand how add a placeholder to username field



this is my html



<form method="post" action=".">
{% csrf_token %}
{% for field in form %}
{{ field }}<br />
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<input id="submit-signup-btn" type="submit" value="Iniciar"/>
</form>









share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to add a placeholder to the username field in the UserCreationForm
    but i can't understand how to do it.
    I have already change the forms.py file in this way /lib/site-packages/django/contrib/auth/forms.py.



    I add a placeholder to password1 and password2 fields and work like this:



    class UserCreationForm(forms.ModelForm):
    """
    A form that creates a user, with no privileges, from the given username and
    password.
    """
    error_messages = {
    'password_mismatch': _("The two password fields didn't match."),
    }
    password1 = forms.CharField(
    label=_("Password"),
    strip=False,
    widget=forms.PasswordInput(attrs={'placeholder':'Password'}),
    help_text=password_validation.password_validators_help_text_html(),
    )
    password2 = forms.CharField(
    label=_("Password confirmation"),
    widget=forms.PasswordInput(attrs={'placeholder':'Confirmar Password'}),
    strip=False,
    help_text=_("Enter the same password as before, for verification."),
    )


    I can see probably the username field is coming from class meta:



    class Meta:
    model = User
    fields = ("username",)
    field_classes = {'username': UsernameField}


    from this class but i'm not sure about that



    class UsernameField(forms.CharField):
    def to_python(self, value):
    return unicodedata.normalize('NFKC', super().to_python(value))


    I don't understand how add a placeholder to username field



    this is my html



    <form method="post" action=".">
    {% csrf_token %}
    {% for field in form %}
    {{ field }}<br />
    {% for error in field.errors %}
    <p style="color: red">{{ error }}</p>
    {% endfor %}
    {% endfor %}
    <input id="submit-signup-btn" type="submit" value="Iniciar"/>
    </form>









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to add a placeholder to the username field in the UserCreationForm
      but i can't understand how to do it.
      I have already change the forms.py file in this way /lib/site-packages/django/contrib/auth/forms.py.



      I add a placeholder to password1 and password2 fields and work like this:



      class UserCreationForm(forms.ModelForm):
      """
      A form that creates a user, with no privileges, from the given username and
      password.
      """
      error_messages = {
      'password_mismatch': _("The two password fields didn't match."),
      }
      password1 = forms.CharField(
      label=_("Password"),
      strip=False,
      widget=forms.PasswordInput(attrs={'placeholder':'Password'}),
      help_text=password_validation.password_validators_help_text_html(),
      )
      password2 = forms.CharField(
      label=_("Password confirmation"),
      widget=forms.PasswordInput(attrs={'placeholder':'Confirmar Password'}),
      strip=False,
      help_text=_("Enter the same password as before, for verification."),
      )


      I can see probably the username field is coming from class meta:



      class Meta:
      model = User
      fields = ("username",)
      field_classes = {'username': UsernameField}


      from this class but i'm not sure about that



      class UsernameField(forms.CharField):
      def to_python(self, value):
      return unicodedata.normalize('NFKC', super().to_python(value))


      I don't understand how add a placeholder to username field



      this is my html



      <form method="post" action=".">
      {% csrf_token %}
      {% for field in form %}
      {{ field }}<br />
      {% for error in field.errors %}
      <p style="color: red">{{ error }}</p>
      {% endfor %}
      {% endfor %}
      <input id="submit-signup-btn" type="submit" value="Iniciar"/>
      </form>









      share|improve this question













      I'm trying to add a placeholder to the username field in the UserCreationForm
      but i can't understand how to do it.
      I have already change the forms.py file in this way /lib/site-packages/django/contrib/auth/forms.py.



      I add a placeholder to password1 and password2 fields and work like this:



      class UserCreationForm(forms.ModelForm):
      """
      A form that creates a user, with no privileges, from the given username and
      password.
      """
      error_messages = {
      'password_mismatch': _("The two password fields didn't match."),
      }
      password1 = forms.CharField(
      label=_("Password"),
      strip=False,
      widget=forms.PasswordInput(attrs={'placeholder':'Password'}),
      help_text=password_validation.password_validators_help_text_html(),
      )
      password2 = forms.CharField(
      label=_("Password confirmation"),
      widget=forms.PasswordInput(attrs={'placeholder':'Confirmar Password'}),
      strip=False,
      help_text=_("Enter the same password as before, for verification."),
      )


      I can see probably the username field is coming from class meta:



      class Meta:
      model = User
      fields = ("username",)
      field_classes = {'username': UsernameField}


      from this class but i'm not sure about that



      class UsernameField(forms.CharField):
      def to_python(self, value):
      return unicodedata.normalize('NFKC', super().to_python(value))


      I don't understand how add a placeholder to username field



      this is my html



      <form method="post" action=".">
      {% csrf_token %}
      {% for field in form %}
      {{ field }}<br />
      {% for error in field.errors %}
      <p style="color: red">{{ error }}</p>
      {% endfor %}
      {% endfor %}
      <input id="submit-signup-btn" type="submit" value="Iniciar"/>
      </form>






      django






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 22:11









      Gabriel costa

      31




      31
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can also define widgets in form's Meta class.



          class Meta:
          model = User
          fields = ("username",)
          field_classes = {'username': UsernameField}
          widgets = {
          'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
          }





          share|improve this answer





















          • I tried that but is not working
            – Gabriel costa
            Nov 11 at 11:32


















          up vote
          0
          down vote













          After a while looking at this, i found a way to do it, this is the way i did it:



          in the file /lib/site-packages/django/contrib/auth/forms.py, in class UserCreationForm, under class Meta there is a init function where you can see this line



          self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
          True})


          i added the placeholder to the end of the line in the update method like this:



          self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
          'placeholder':'Nome Perfil'})





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243932%2fhow-to-add-placeholder-usercreationform-django%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You can also define widgets in form's Meta class.



            class Meta:
            model = User
            fields = ("username",)
            field_classes = {'username': UsernameField}
            widgets = {
            'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
            }





            share|improve this answer





















            • I tried that but is not working
              – Gabriel costa
              Nov 11 at 11:32















            up vote
            0
            down vote













            You can also define widgets in form's Meta class.



            class Meta:
            model = User
            fields = ("username",)
            field_classes = {'username': UsernameField}
            widgets = {
            'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
            }





            share|improve this answer





















            • I tried that but is not working
              – Gabriel costa
              Nov 11 at 11:32













            up vote
            0
            down vote










            up vote
            0
            down vote









            You can also define widgets in form's Meta class.



            class Meta:
            model = User
            fields = ("username",)
            field_classes = {'username': UsernameField}
            widgets = {
            'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
            }





            share|improve this answer












            You can also define widgets in form's Meta class.



            class Meta:
            model = User
            fields = ("username",)
            field_classes = {'username': UsernameField}
            widgets = {
            'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 1:55









            Uroš Trstenjak

            523413




            523413












            • I tried that but is not working
              – Gabriel costa
              Nov 11 at 11:32


















            • I tried that but is not working
              – Gabriel costa
              Nov 11 at 11:32
















            I tried that but is not working
            – Gabriel costa
            Nov 11 at 11:32




            I tried that but is not working
            – Gabriel costa
            Nov 11 at 11:32












            up vote
            0
            down vote













            After a while looking at this, i found a way to do it, this is the way i did it:



            in the file /lib/site-packages/django/contrib/auth/forms.py, in class UserCreationForm, under class Meta there is a init function where you can see this line



            self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
            True})


            i added the placeholder to the end of the line in the update method like this:



            self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
            'placeholder':'Nome Perfil'})





            share|improve this answer

























              up vote
              0
              down vote













              After a while looking at this, i found a way to do it, this is the way i did it:



              in the file /lib/site-packages/django/contrib/auth/forms.py, in class UserCreationForm, under class Meta there is a init function where you can see this line



              self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
              True})


              i added the placeholder to the end of the line in the update method like this:



              self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
              'placeholder':'Nome Perfil'})





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                After a while looking at this, i found a way to do it, this is the way i did it:



                in the file /lib/site-packages/django/contrib/auth/forms.py, in class UserCreationForm, under class Meta there is a init function where you can see this line



                self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
                True})


                i added the placeholder to the end of the line in the update method like this:



                self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
                'placeholder':'Nome Perfil'})





                share|improve this answer












                After a while looking at this, i found a way to do it, this is the way i did it:



                in the file /lib/site-packages/django/contrib/auth/forms.py, in class UserCreationForm, under class Meta there is a init function where you can see this line



                self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
                True})


                i added the placeholder to the end of the line in the update method like this:



                self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
                'placeholder':'Nome Perfil'})






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 15:13









                Gabriel costa

                31




                31






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243932%2fhow-to-add-placeholder-usercreationform-django%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown