Как заставить мою crispy-форму работать в Django в процессе входа в систему?

Я хотел бы настроить мою страницу входа в систему с помощью crispy, используя заполнители в текстовых полях вместо меток. Я не могу войти в систему с помощью тега crispy form, и дизайн также отличается от того, что я хочу, но с помощью фильтра crispy я могу войти в систему, но я не знаю, как разработать шаблон в соответствии с моими пожеланиями.

Вот мой код на данный момент:

views.py:

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import UserRegisterForm, UserIndexForm

#The webpage where you can login.

def login(request):

    return render(request, 'loginprofile/login.html')

forms.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Field

# UserIndexForm

class UserIndexForm(AuthenticationForm):
    
    def __init__(self, *args, **kwargs):
        super(UserIndexForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False
        self.helper.form_show_errors = True
        self.helper.error_text_inline = False
        self.helper.help_text_inline = True
        self.helper.form_tag = False
        self.helper.form_method = 'POST'
        self.helper.form_action = 'submit_survey'

        self.fields['username'].help_text = None
        self.fields['password'].help_text = None
        self.helper.layout = Layout(
            Field('username', placeholder='Username'),
            Field('password', placeholder='Password'),
        )

        self.helper.add_input(Submit('submit', 'Sign In', css_class='container btn-success justify-content-center'))

    class Meta:
        model = User
        fields = ['username', 'password']

        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control mb-0', 'placeholder': 'Username'}),
            'password': forms.TextInput(attrs={'class': 'form-control mb-0', 'placeholder': 'Password'}),
        }

settings.py:

# Crispy-Settings

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = 'bootstrap5'

# Login-Logut-System

LOGIN_REDIRECT_URL = 'dashboard'
#LOGOUT_REDIRECT_URL = 'homepage'
LOGIN_URL = 'login'

urls.py:

from django.urls import path
from loginprofile import views as loginprofile_views
from django.contrib.auth import views as auth_views

from loginprofile.forms import UserIndexForm

urlpatterns = [
    path("", loginprofile_views.homepage, name="homepage"),
    path("login", auth_views.LoginView.as_view(template_name='loginprofile/login.html'), name="login"),
    path("logout", auth_views.LogoutView.as_view(template_name='loginprofile/logout.html'), name="logout"),
    path("register", loginprofile_views.register, name="register"),
    path("recover", loginprofile_views.passwordRecover, name="recover"),
    path("dashboard", loginprofile_views.dashboard, name="dashboard"),
]

шаблон login.html:

{% extends "base-footer.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <div class="container min-vh-100 d-flex justify-content-center align-items-center">
            <div class="text-start text-light bg-secondary rounded-3 px-4 py-4 border border-dark">
                <form action="" method="POST" class="">
                    {% csrf_token %}
                    <fieldset class="form-group">
                        <legend class="border-bottom pe-3 mb-4">
                            <a href="{% url 'homepage' %}" class="link-light px-1 me-2 text-dark">
                                <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-arrow-left mb-1" viewBox="0 0 16 16">
                                    <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8"/>
                                </svg></a>Sign In
                        </legend>
                        <div class="container px-4">
                            {{ form|crispy }}<!--  -->
                            <!-- {% crispy form %} -->
                        </div>
                    </fieldset>
                    <div class="container form-group px-4 py-2 text-end">
                        <button class='container btn btn-success' type="submit">Login</button>
                    </div>
                </form>
                <div class="container justify-content-center align-middle pt-0 text-start">
                    <div class="container">
                        <small class="text-muted-light">
                            No account? <a class="ms-2 link-light link-underline-dark text-dark" href="{% url 'register' %}">Sign Up</a>
                        </small>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock content %}

Я много искал и много пробовал, но пока не нашел ничего подходящего для своей проблемы.

Вернуться на верх