将 Crispy Forms 与 Django 结合使用,在使用 Crispy Forms 的任何功能时,我只能收到
TemplateDoesNotExist
错误。
由于我是 Crispy Forms 的新手(这似乎被普遍推荐用于快速使表单看起来更好),我已按照https://django-crispy-forms.readthedocs.io/en/latest/install.html中的说明进行操作,据我所知,安装是正确的(使用
pip
安装并在settings.py
中进行更改)。我在 Windows 计算机上的虚拟环境(下面提到的 .venv
文件夹)中运行它。
我什至专门创建了一个新项目来研究这个问题,内容绝对很少,但同样的问题仍然存在。该项目称为“stuff”,其中的单个应用程序称为“other”。
设置.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'
...
模型.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})
测试.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>
点冻结的结果
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 看不到应该安装的模板。或者也许我还应该下载或创建其他东西?
我想在处理更紧迫的问题之前快速整理 Django 项目中的一个表单,几个小时后我仍然无法让 Crispy Forms 完全运行(如果用其他方式对此进行排序会更快)。很明显我错过了一些东西,但是什么呢?
我尝试过的其他奇怪而美妙的事情
并非所有这些都符合逻辑,但是嘿!
CRISPY_TEMPLATE_PACK
中放入错误的字符串 - 这会导致错误,提示我需要使用 bootstrap3
、bootstrap4
或 uni_form
。尽管重复这个实验仍然会出现上面报告的错误(拼写错误)django-bootstrap4
模块并从CDN加载(没有区别){% crispy aform %}
- 相同的结果从 django-crispy-forms 2.0 开始,模板包现在位于单独的包中。
您需要
pip install crispy-bootstrap4
并将 crispy_bootstrap4
添加到您的 INSTALLED_APPS
列表中。
如果您需要使用bootstrap4
在主应用程序的settings.py里面添加
INSTALLED_APPS = [ ... 'crispy_forms', 'crispy_bootstrap4', ... ]
和CRISPY_TEMPLATE_PACK = 'bootstrap4'
在 your_html.html 文件中添加
{% load crispy_forms_tags %}
并且您可以根据需要使用cripy_forms
我遇到了同样的问题并得到了解决方案。这是因为您正在使用 django-crispy-forms==2.0 并且您的项目与此版本不兼容。另外这个 django-crispy-forms==2.0 不支持 bootstrap4.
解决方案: 因此安装以下版本的软件包: (确保Python版本> 3.7)