You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Django Crispy Forms时出现TemplateDoesNotExist错误求助

Django + Crispy Forms 出现 TemplateDoesNotExist 错误的问题

问题现象

使用Django搭配Crispy Forms时,只要调用Crispy Forms的任意功能(比如{{ aform|crispy }}或{% crispy aform %}),就会触发TemplateDoesNotExist错误,提示找不到bootstrap4/uni_form.html。

已按照官方文档完成安装(pip安装并修改settings.py),在Windows虚拟环境中运行,特意创建极简新项目(项目名stuff,仅含应用other)排查,问题依旧。

相关配置信息

settings.py

...

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'other',
    'bootstrap4'
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

...

models.py

from django.db import models

class Mine(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()

forms.py

from django import forms
from .models import Mine

class MineForm(forms.ModelForm):
    class Meta:
        model = Mine
        fields = ('name','email')

views.py

from django.shortcuts import render
from .forms import *

def idx(request):
    tform = MineForm()

    return render(request,'test.html',{'aform': tform})

test.html模板

{% load bootstrap4 %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TestThing</title>
</head>
<body>
    <form action="/">
        {% csrf_token %}
        {{ aform|crispy }}
    </form>
</body>
</html>

依赖版本(pip freeze)

asgiref==3.6.0
beautifulsoup4==4.11.2
Django==4.1.7
django-bootstrap4==22.3
django-crispy-forms==2.0
soupsieve==2.4
sqlparse==0.4.3
tzdata==2022.7

错误详情

TemplateDoesNotExist at /
bootstrap4/uni_form.html

模板加载路径排查

Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: C:\ProjectDir\.venv\lib\site-packages\django\contrib\admin\templates\bootstrap4\uni_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProjectDir\.venv\lib\site-packages\django\contrib\auth\templates\bootstrap4\uni_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProjectDir\other\templates\bootstrap4\uni_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProjectDir\.venv\lib\site-packages\bootstrap4\templates\bootstrap4\uni_form.html (Source does not exist)

已尝试的无效操作

  • 故意在CRISPY_TEMPLATE_PACK中填写错误值,报错提示支持bootstrap3、bootstrap4或uni_form,但改回正确值后问题依旧
  • 移除django-bootstrap4模块,改用CDN加载bootstrap资源,无变化
  • 在模板路径手动创建空白的bootstrap4/uni_form.html文件,表单仍无法正常渲染
  • 改用{% crispy aform %}标签替代过滤器,报错内容完全一致

疑问

看起来Crispy Forms找不到本该随包安装的模板文件,是否需要额外下载或创建模板?明明只想快速优化表单样式,却耗时数小时无法解决,到底遗漏了什么步骤?


内容的提问来源于stack exchange,提问作者Dave

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:02:57