Django在使用Crispy Forms时返回’TemplateDoesNotExist’错误。

huangapple go评论57阅读模式
英文:

Django returns 'TemplateDoesNotExist' when using Crispy Forms

问题

使用Django和Crispy Forms时,当使用Crispy Forms的任何功能时,我只能得到一个TemplateDoesNotExist错误。

由于我对Crispy Forms还很陌生(它似乎是普遍推荐的用于快速美化表单的工具),我已经按照https://django-crispy-forms.readthedocs.io/en/latest/install.html上的说明进行了安装,并就我所知进行了正确的安装(使用pip安装并在settings.py中进行了更改)。我在Windows机器上的虚拟环境(下面提到的.venv文件夹)中运行这个项目。

我甚至创建了一个全新的项目,只包含绝对最小的内容,但仍然存在相同的问题。该项目名为'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事后剖析

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 - 这会导致一个错误,建议我使用bootstrap3bootstrap4uni_form。尽管重复此实验会保留上面报告的错误(拼写错误)。
  • 删除django-bootstrap4模块并从CDN加载(没有区别)。
  • 在显示模板的路径下创建一个空的HTML文件,它只是无法呈现表单。
  • 使用{% crispy aform %} - 结果相同。
英文:

Using Crispy Forms with Django, I can only get a TemplateDoesNotExist error when using any feature of Crispy Forms.

As I'm new to Crispy Forms (which seems to be universally recommended for quickly making forms look better), I have followed the instructions at https://django-crispy-forms.readthedocs.io/en/latest/install.html and as far as I know, the installation is correct (installed using pip and changes in settings.py). I am running this in a virtual environment (the .venv folder referred to below) on a Windows machine.

I have even created a new project specifically to look at this, with absolutely minimal content, and the same problem persists. The project is called 'stuff' and the single app in it 'other'.

settings.py

...

INSTALLED_APPS = [
    &#39;django.contrib.admin&#39;,
    &#39;django.contrib.auth&#39;,
    &#39;django.contrib.contenttypes&#39;,
    &#39;django.contrib.sessions&#39;,
    &#39;django.contrib.messages&#39;,
    &#39;django.contrib.staticfiles&#39;,
    &#39;crispy_forms&#39;,
    &#39;other&#39;,
    &#39;bootstrap4&#39;
]

CRISPY_TEMPLATE_PACK = &#39;bootstrap4&#39;

...

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 = (&#39;name&#39;,&#39;email&#39;)

views.py

from django.shortcuts import render
from .forms import *

def idx(request):
    tform = MineForm()

    return render(request,&#39;test.html&#39;,{&#39;aform&#39;: tform})

test.html

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

results of 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

error reported on debug page

TemplateDoesNotExist at /
bootstrap4/uni_form.html

Template-loader postmortem

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)

It looks to me like Crispy can't see the templates which should have been installed. Or perhaps there's something else I'm supposed to download or create?

I wanted to quickly tidy a form up in my Django project before moving on to more pressing matters, and hours later I still can't get Crispy Forms to function at all (it would have been quicker to sort this in other ways). It's clear I'm missing something, but what?

Other Weird and Wonderful things I've tried

Not all of these might be logical, but hey!

  • deliberately putting the wrong string in CRISPY_TEMPLATE_PACK -- this results in an error suggesting I need to use bootstrap3, bootstrap4, or uni_form. Although repeating this experiment keeps the error reported above (with the misspelling)
  • removing the django-bootstrap4 module and loading from CDN (no difference)
  • creating a blank HTML file at the path shown for the template, which just fails to render the form
  • using {% crispy aform %} - same result

答案1

得分: 62

你需要pip install crispy-bootstrap4并将crispy_bootstrap4添加到你的INSTALLED_APPS列表中。

英文:

As of django-crispy-forms 2.0 the template packs are now in separate packages.

You will need to pip install crispy-bootstrap4 and add crispy_bootstrap4 to your list of INSTALLED_APPS.

答案2

得分: 13

在需要使用Bootstrap 4 的情况下:

  1. 安装 django-crispy-forms
pip install django-crispy-forms
  1. 安装 crispy-bootstrap4
pip install crispy-bootstrap4

在主应用的 settings.py 中添加以下内容:

INSTALLED_APPS = [
...
'crispy_forms',
'crispy_bootstrap4',
...
] 

CRISPY_TEMPLATE_PACK = 'bootstrap4'

在你的 your_html.html 文件中添加以下代码:

{% load crispy_forms_tags %}

然后你可以像喜欢的方式使用 crispy_forms

英文:

if you need to use bootstrap4

  1. pip install django-crispy-forms
  2. pip install crispy-bootstrap4

inside settings.py in the main app add
INSTALLED_APPS = [
...
&#39;crispy_forms&#39;,
&#39;crispy_bootstrap4&#39;,
...
]

and CRISPY_TEMPLATE_PACK = &#39;bootstrap4&#39;

inside your_html.html file add
{% load crispy_forms_tags %} and you can use cripy_forms as you like

答案3

得分: 0

我遇到了相同的问题并找到了解决方案。这是因为您正在使用 django-crispy-forms==2.0,而您的项目与这个版本不兼容。此外,django-crispy-forms==2.0 不支持 bootstrap4

解决方案:
因此,请安装以下指定版本的软件包:(确保 Python 版本 > 3.7)

  • pip3 install Django==4.1.6
  • pip3 install django-allauth==0.52.0
  • pip3 install django-crispy-forms==1.14.0 # 版本 <= 1.70
  • pip3 install django-embed-video
英文:

I faced same issue and got solution. This is because you are using django-crispy-forms==2.0 and your project is not compatible with this version. also this django-crispy-forms==2.0 not support bootstrap4.

SOLUTION:
so install packages with below mention version:
(make sure python version > 3.7)

  • pip3 install Django==4.1.6
  • pip3 install django-allauth==0.52.0
  • pip3 install django-crispy-forms==1.14.0 # version <=1.70
  • pip3 install django-embed-video

答案4

得分: 0

  1. 运行以下命令:

    pip install django-crispy-forms
    
  2. 进入 settings.py 并在已安装的应用中添加以下内容(不要忘记逗号):

    "crispy_forms",
    "crispy_bootstrap4"
    
  3. 在已安装的应用后添加以下内容(这里不需要逗号):

    CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"
    CRISPY_TEMPLATE_PACK = 'bootstrap4'
    
  4. 进入要添加 crispy 表单的 HTML 页面,并在头部部分添加以下内容:

    {% load crispy_forms_tags %}
    
  5. 最后在正文部分添加以下内容:

    {{form|crispy_field}} 
    
英文:
  1. Run this command:

    pip install django-crispy-forms
    
  2. Go to settings.py and add this in the installed apps (don't forget the comma):

    &quot;crispy_forms&quot;,
    &quot;crispy_bootstrap4&quot;
    
  3. Add this after the installed apps (here there's no need for a comma):

    CRISPY_ALLOWED_TEMPLATE_PACKS = &quot;bootstrap4&quot;
    CRISPY_TEMPLATE_PACK = &#39;bootstrap4&#39;
    
  4. Go to the html page where you want to add the crispy form and add this in the head section:

    {% load crispy_forms_tags %}
    
  5. Finally add this to the body section:

    {{form|crispy_field}} 
    

huangapple
  • 本文由 发表于 2023年2月19日 02:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定