How to map the login template with built-in login form in Django?

I am quite new to Django and I am trying to create login template. I avoid using crispy-forms to have more control and knowledge on how the data is displayed on website. I am using Django built-in login form. Unfortunately I am stuck, because my current login.html seems to be invalid and nothing happens after sending POST with username and password. This is how it looks like:

login.html

    <main class="form-signin">
      <form method="post" class="from-group">
        <h1 class="display-3 text-center mb-4 fw-normal" style="font-family:'Faster One'; box-shadow: none;">Test</h1>
        <h1 class="h3 mb-3 fw-normal">Please sign in</h1>
        {% csrf_token %}
        <div class="form-floating">
          <input type="text" class="form-control" id="floatingInput" placeholder="name@example.com">
          <label for="floatingInput">Username</label>
        </div>
        <div class="form-floating mt-3">
          <input type="password" class="form-control" id="floatingPassword" placeholder="Password">
          <label for="floatingPassword">Password</label>
        </div>
        <button class="w-100 btn btn-lg btn-primary" type="submit" value="Log in">Sign in</button>
      </form>
    </main>

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('accounts/', include("django.contrib.auth.urls")),
]

views.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render
from poolcar.forms import ReservationForm


@login_required()
def home(request):
    return render(request, 'index.html')

[23/Apr/2022 09:59:41] "POST /accounts/login/ HTTP/1.1" 200 2358

[23/Apr/2022 09:59:46] "POST /accounts/login/ HTTP/1.1" 200 2358

POST is sent with 200 OK message, but besides that nothing happens - I am still in the login page, user is not authenticated, validation doesn't work etc. So my question is what I did wrong and how does the mapping between login form/view and html template works exactly?

The html looping syntax of form is following, where we have access to specific field, field.label ,non_field_errors as well as particular field errors.

In your case you can use in this way:

login.html

<main class="form-signin">
  <form method="post" class="from-group">
    <h1 class="display-3 text-center mb-4 fw-normal" style="font-family:'Faster One'; box-shadow: none;">Test</h1>
    <h1 class="h3 mb-3 fw-normal">Please sign in</h1>
        {% csrf_token %}
        {% if form.non_field_errors %}
            {% for error in form.non_field_errors  %}
            <div>
                {{error}}
            </div>
            {% endfor %}
        {% endif %}

        {% for field in form  %}
        <p>{{field.label_tag}} {{field}}</p>
        <br>
            {% for error in field.errors  %}
                <span>{{error}}</span>
            {% endfor %}
        {% endfor %}
    <button class="w-100 btn btn-lg btn-primary" type="submit" value="Log in">Sign in</button>
  </form>
</main>

And you can define input class and id like this:

forms.py

class MyForm(forms.Form):
    name=forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control','id': 'floatingInput'}))

If you could also post views.py it would be better

Back to Top