在Django中引入Bootstrap

首先

这次我们将介绍如何在Django中引入Bootstrap。
而且,在引入Bootstrap之后,我们将应用于之前创建的登录页面,在“给Django的Web应用程序添加用户认证(django-allauth)”中。

2. 安装django-bootstrap5

我們將使用pip來安裝Django-Bootstrap5。

pip install django-bootstrap5

更改settings.py文件

在settings.py文件的INSTALLED_APPS中添加django_bootstrap5。

INSTALLED_APPS = [
    ...
    'django_bootstrap5', # 追加
]

4. 应用于模板。

在模板的开头行添加以下代码。

{% load django_bootstrap5 %}

在模板的头部添加以下代码。

<head>
    ...
    {% bootstrap_css %}
    {% bootstrap_javascript %}
</head>

用这个就完成了Bootstrap的导入。

5. 自定义 django-allauth 的模板

要自定义django-allauth的模板文件,您可以将django-allauth的模板文件复制到您自己的templates目录下进行编辑。
您可以使用pip show命令来确认django-allauth的位置。安装路径会在Location中列出。

$ pip show django-allauth
Name: django-allauth
Version: 0.51.0
Summary: Integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication.
Home-page: http://www.intenct.nl/projects/django-allauth/
Author: Raymond Penners
Author-email: raymond.penners@intenct.nl
License: UNKNOWN
Location: /home/vagrant/.local/lib/python3.8/site-packages
Requires: requests-oauthlib, Django, requests, python3-openid, pyjwt
Required-by:

6. Bootstrap的應用後的畫面

应用Bootstrap并自定义了django-allauth模板后的结果如下:

after1.png
after2.png
after3.png

7. 最后 总结

这次我在Django中引入了Bootstrap,并进行了设计改进。
今后我还想继续发布有关Django学习内容的帖子。

bannerAds