2

Here is the code of that form:

<div>
<h2>Login here</h2>
    <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Login</button>
    </form>
</div>

This form comes from django.contrib.auth

2
  • This is a rather broad question. What have you tried? Are you just asking about how to apply CSS to a django implementation? Is there a reason you can't simply add CSS classes to the elements? Commented Dec 4, 2019 at 2:12
  • I am totally new to Django and it's implementations. First I tried to find where I can edit the code of built-in form - no go. Now I think that I could apply classes to the code that I added there, but what if I want to apply different styles to log in and password fields? Commented Dec 4, 2019 at 2:20

2 Answers 2

2

You can assign both HTML ID and classes to your form fields using the widget attributes. I put an example below of setting both the ID and class, but be careful with the ID's because Django auto creates them. The ID and class names must match your CSS file of course.

class MyForm(forms.ModelForm):

    class Meta:
        model = User
        fields = ('email', 'password')
        # Set custom ID and class for password field.
        widgets = {'password': forms.PasswordInput(attrs={'id': 'my_HTML_id', 'class': 
                   'my_HTML_class'})}
Sign up to request clarification or add additional context in comments.

Comments

2

You can manually render the login form but make sure that username field name is username and password is password. In Django you can also add custom login functionality with custom checks. The authenticate(request, username=act_username, password=password) is used to authenticate the user where login is to login the user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.