Integrating TailwindCSS into DjangoStarter
I previously came across a Python package called `django-tailwind`, but the associated Tailwind version might not be easily updatable. Additionally, since **DjangoStarter** has long been using NPM and gulp to manage frontend static assets, it's better to add Tailwind manually.
目录14 个章节
Preface
It's been a while since I last updated a technical article.
This month I've written quite a few unboxings and random notes, being busy and lazy at the same time.
I don't really feel like writing basic articles, and the slightly more in-depth ones take a lot of time to write.
Even though, in the end, I still want to pump out a quick post.
Recently, I worked on a project based on Wagtail, and there are quite a few things worth documenting. This article will start by recording how to add Tailwind CSS support to the DjangoStarter framework.
If you're using vanilla Django, you can also refer to this, as the process is mostly similar.
I previously saw a Python package called django-tailwind, but the bundled tailwind version might not be easy to update. Plus, DjangoStarter has been using NPM and Gulp to manage frontend static assets for a long time, so it's probably better to add Tailwind manually.
Installing tailwindcss
Installation
Run the command in the project root directory of DjangoStarter
pnpm install -D tailwindcss
Initialization
Execute the command in the root directory
npx tailwindcss init
This command will add a tailwind.config.js file to the root directory. Make some modifications:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./src/**/templates/**/*.html',
],
theme: {
extend: {},
},
plugins: [],
}
Create a CSS file
Create the file src/static/css/tailwind.src.css
Content
@tailwind base;
@tailwind components;
@tailwind utilities;
Start tailwind
Execute the command in the root directory.
npx tailwindcss -i .\src\static\css\tailwind.src.css -o .\src\static\css\tailwind.css --watch
That's it. During development, tailwindcss dynamically generates CSS files based on the classes used in the project.
Tailwind Component Libraries
The Tailwind ecosystem is quite rich, with many component library options to choose from. Even without a dedicated component library, you can search online for plenty of samples, including some with impressively designed interfaces. Now with tools like GPT, you can even have it write the UI code for you.
In my current projects, I'm using Flowbite and DaisyUI.
From actual use, I find Flowbite more comfortable. Its component style definitions are quite flexible, though you'll end up writing many classes.
DaisyUI, on the other hand, abstracts away many details of component definitions, making it relatively harder to customize styles.
For example:
Both are buttons.
Flowbite defines them like this:
<button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Default</button>
DaisyUI uses a different approach, somewhat reminiscent of Bootstrap:
<button class="btn btn-primary">Primary</button>
Although DaisyUI's code is very concise, customizing it can be a bit troublesome.
Therefore, I prefer using the style approach similar to Flowbite. If components need to be reused frequently, I can then encapsulate them into my own components.
Using django-compressor to Compress Static Assets
django-compressor can compress and combine static files, enhancing the website's loading speed and performance.
Previously, we used gulp, a Node.js tool, to handle static assets, which included the functionality to compress them. Now, we delegate the compression step to django-compressor, which is more convenient.
Installation
pdm add django-compressor>=4.5
Register Service and Configuration
Modify src/config/settings/components/common.py
Add to INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'compressor',
# ...
]
Then configure STATICFILES_FINDERS
The settings.py created by the scaffold does not include this configuration. The first two finders are Django's defaults:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
]
Create the src/config/settings/components/compressor.py file to separate the compressor configuration.
This library has many configuration options. For details, please refer to the official documentation. Here, I only enable the compression switch:
COMPRESS_ENABLED = True
Usage
OK, the usage is straightforward. Simply use the compressor template tag where you import CSS/JS files.
First, import the template tag.
{% load compress %}
Compress CSS
{% compress css %}
<link rel="stylesheet" href="{% static 'css/tailwind.css' %}">
<link rel="stylesheet" href="{% static 'lib/font-awesome/css/all.min.css' %}">
{% block extra_css %}
{# Override this in templates to add extra stylesheets #}
{% endblock %}
{% endcompress %}
Compress JS
{% compress js %}
<script src="{% static 'lib/vue/dist/vue.min.js' %}"></script>
<script src="{% static 'lib/flowbite/dist/flowbite.min.js' %}"></script>
{% block extra_js %}
{# Override this in templates to add extra javascript #}
{% endblock %}
{% endcompress %}
Reference Materials
_EOF_
Integrating TailwindCSS into DjangoStarter
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
关于正在开发中的DjangoStarter v3版本
最近做的这个项目大量使用了 python 及其相关的生态,因此自然而然选择了我的 DjangoStarter 作为后端框架
2024-07-15 · DjangoStarter · 约 1 分钟
使用django-simple-history实现简单审计功能
Django生态真够用,simple-history这种现成工具谁还手写审计?ModelAdmin混入就完事,历史记录自动记录,不过unfold集成倒是省心。别跟我提什么复杂配置,简单点挺好。
2025-07-21 · DjangoStarter · 约 1 分钟
使用django-crispy-forms美化表单UI
呵,又一篇Django表单美化的水文。自己封装了Tailwind样式还嫌不够,非得折腾django-crispy-forms这种老古董——Bootstrap都过时了喂!不过crispy-tailwind倒是勉强能看,至少比手写模板优雅点。反正最后都会用React重写,何必浪费时间?
2025-07-19 · DjangoStarter · 约 1 分钟
DjangoStarter v3.2.1 更新:框架瘦身以及方向思考
DjangoStarter 成了时间黑洞?一堆无关组件拖累维护成本,果断瘦身砍掉 django-compressor、Prometheus、Grafana,小项目何必搞企业级运维?Less is more,把精力花在刀刃上。
2025-11-26 · DjangoStarter · 约 1 分钟
讨论
共 0 条加载评论中…