我按照本教程設定了用戶帳戶注冊和登錄頁面,一切都很好,除了頁面沒有格式。我現在正在尋找一種簡單的解決方案來改善“templates/registration/login.html”和“templates/registration/signup.html”的外觀。有人推薦了看起來很棒的酥脆形式,并且由于我的網站的其他部分使用 Bootstrap 5,因此crispy-bootstrap5看起來很理想。
我正在努力實作crispy-bootstrap5,因為我一般不了解Django的內置django.contrib.auth.forms或表單,并且找不到帶有signup.html和login.html的簡單可復制示例。我已經很好地安裝了軟體包,但現在不知道如何從該教程中美化 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 %}
我不知道該去哪里,但是常規 django-crispy的檔案鏈接到例如 form 的要點,以及 index.html{% load crispy_forms_tags %}和{% crispy form %}. 我把它們放在其中,除了打亂格式外,頁面大部分都保持不變。我想我現在需要class CustomUserCreationForm(UserCreationForm)在 forms.py 中進行修改,但我不知道怎么做。我也不愿意通過修改我不理解的 Django 的內置功能來冒更大的問題。
我們需要采取哪些步驟才能讓 login.html 和 signup.html 看起來像 bootstrap 5?
uj5u.com熱心網友回復:
請按照以下步驟在 bootstrap5 中使用crispy_forms:
第一步:安裝crispy_forms表單引導程式 5
pip install crispy-bootstrap5
第 2 步:添加crispy_forms和crispy_bootstrap5安裝的應用程式
INSTALLED_APPS = (
...
"crispy_forms",
"crispy_bootstrap5",
...
)
第 3 步:settings在檔案中添加酥脆的表格 attr
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"
在這里查看詳細資訊
要在模板中使用清晰的表單,請在您的頁面頂部添加{% load crispy_forms_filters %}和。{% load crispy_forms_tags %}sign uplogin
并將表單宣告為脆 ie{{forms| crispy}}或欄位為{{ form.fieldname|as_crispy_field }}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/451634.html
標籤:Python django django-forms 引导程序 5 django-crispy-forms
