添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 = [ '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">
    <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>
    <form action="/">
        {% csrf_token %}
        {{ aform|crispy }}
    </form>
</body>
</html>

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
  • The error at the bottom of the post tells you all the directories where it looked, and couldn't find, the template. So what is the actual location of the template? – John Gordon Feb 18 at 19:13 @john Unfortunately what it's looking for is the uni_form.html file, which is part of the Crispy Forms system. Creating a blank file with that name in one of those paths gets rid of the error, and renders a page with no form. All references I've found so far to Crispy Forms seem to suggest doing the pip install, installing in the project, and using the {% load crispy_forms_tags %} tag at the top of the template, with the form piped through crispy. There has been no suggestion of having to manually create a template file, or install one from elsewhere. – Dave Feb 18 at 21:37 Does the problem still exist when you remove bootstrap4 pip uninstall bootstrap4 and remove it from your INSTALLED_APPS? – Marco Feb 19 at 0:11 @Marco Unfortunately, it persists even when doing that (and obviously loading bootstrap manually) – Dave Feb 19 at 5:24 Have you set "APP_DIRS" to True in TEMPLATE variable in settings.py? Try setting it to False. In my project it was set to False to prevent Django to look at the default locations in each app redundantly as all my templates and static file live at project level. BTW, I'm using bootstrap5 downloaded locally and not django's built in package. Anyway i had the same error and modifying back to False resolved my issue – udarH3 Feb 19 at 12:53 Thank you - that is exactly the solution. I've had another look, and can't see anything pointing in that direction on the Crispy Forms site (in fact it says "Since version 2.0, django-crispy-forms has built-in support for version 3 and 4 of the Bootstrap CSS framework"). – Dave Feb 19 at 21:45 The erroneous text in the documentation was fixed on 24th March: github.com/django-crispy-forms/django-crispy-forms/commit/… – interDist Jun 25 at 8:02

    inside settings.py in the main app add INSTALLED_APPS = [ ... 'crispy_forms', 'crispy_bootstrap4', ... ] and CRISPY_TEMPLATE_PACK = 'bootstrap4'

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

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.