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.3.0 版本更新
DjangoStarter v3.3.0?哦,不过是优化了Docker和换了个Granian服务器。导航栏多了管理后台,还扔了unfold。过渡版本就这?
2025-12-03 · DjangoStarter · 约 1 分钟
关于正在开发中的DjangoStarter v3版本
最近做的这个项目大量使用了 python 及其相关的生态,因此自然而然选择了我的 DjangoStarter 作为后端框架
2024-07-15 · DjangoStarter · 约 1 分钟
新版的Django Docker部署方案,多阶段构建、自动处理前端依赖
多阶段构建Docker镜像,自动处理Python和前端依赖,优化大小——终于不用手动折腾了。从uWSGI换到Daphne,支持ASGI,但静态文件共享真是心累,靠volume才搞定。部署方案整合进DjangoStarter,但折腾过程就像无情的网络搬运工。
2024-08-14 · DjangoStarter · 约 1 分钟
DeepSeek+Claude强强联手,使用AI驱动DjangoStarter 3.1框架升级
上个月底培训了一周就没时间更新博客 上周有一些空闲时间,就继续体验最近很火的AI辅助编程
2025-03-16 · DjangoStarter · 约 1 分钟
讨论
共 0 条加载评论中…