在默认注册/登录页面中实现 Django Bootstrap 脆皮表单?

Implementing Django Bootstrap crispy forms into default signup / login pages?

我已经设置了用户帐户注册和登录页面,this tutorial and it all works great, except the pages have no formatting. I'm now looking for a simple drop in solution to improve the appearance of "templates/registration/login.html" and "templates/registration/signup.html". Someone recommended crispy forms which look great, and as the rest of my site uses Bootstrap 5, crispy-bootstrap5 看起来很理想。

我正在努力实现 crispy-bootstrap5,因为我不理解 Django 的内置 django.contrib.auth.forms 和一般的形式,并且找不到简单的可重现的 crispy 形式的例子 signup.html 和 login.html。我 installed packages 很好,但现在不知道如何美化那个教程中的 login.html 和 signup.html:

{% extends 'base.html' %}

{% block title %}Sign Up{% endblock %}

{% block content %}
  <h2>Sign up</h2>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Sign Up</button>
  </form>
{% endblock %}

我不知道该去哪里,但是 regular django-crispy links to a gist for an eg form、index.html、{% load crispy_forms_tags %}{% crispy form %} 的文档。我把它们放在其中,除了弄乱格式外,大部分页面都保持不变。我想我现在需要修改 forms.py 中的 class CustomUserCreationForm(UserCreationForm) 但我不知道如何修改。我也不愿意通过修改我不理解的 Django 内置功能来冒更大的问题。

我们需要采取哪些步骤才能使 login.html 和 signup.html 看起来像 bootstrap 5?

请按照以下步骤使用 crispy_forms 和 bootstrap5:

第一步:安装crispy_forms表格bootstrap5

pip install crispy-bootstrap5

第 2 步:crispy_formscrispy_bootstrap5 添加到已安装的应用程序

INSTALLED_APPS = (
    ...
    "crispy_forms",
    "crispy_bootstrap5",
    ...
)

第 3 步: 将 crispy 表单属性添加到 settings 文件

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"

CRISPY_TEMPLATE_PACK = "bootstrap5"

查看here了解详情

要在模板中使用脆皮形式,请在 sign uplogin 页面顶部添加 {% load crispy_forms_filters %}{% load crispy_forms_tags %}

并将表单声明为 crispy 即 {{forms| crispy}} 或字段为 {{ form.fieldname|as_crispy_field }}