pythondjangodjango-crispy-forms

TemplateDoesNotExist at /users/register/ bootstrap5/uni_form.html


I am building a registration form for my django project, and for styling it I am using crispy forms. But, when I run my server and go to my registration page, I see this error:

    Internal Server Error: /users/register/
Traceback (most recent call last):
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 988, in render
    output = self.filter_expression.resolve(context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\base.py", line 698, in resolve
    new_obj = func(obj, *arg_vals)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py", line 60, in as_crispy_form
    template = uni_form_template(template_pack)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py", line 21, in uni_form_template
    return get_template("%s/uni_form.html" % template_pack)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: bootstrap5/uni_form.html

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Dell\Desktop\Django\microblog\microblog_project\users\views.py", line 17, in register
    return render(request, 'users/register.html',context)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\backends\django.py", line 63, in render
    reraise(exc, self.backend)
  File "C:\Users\Dell\Desktop\Django\microblog\venv\lib\site-packages\django\template\backends\django.py", line 84, in reraise
    raise new from exc
django.template.exceptions.TemplateDoesNotExist: bootstrap5/uni_form.html

This doesn't look like the usual TemplateDoesNotExistError I get. I think this is the error in my crispy form, because if I remove the crispy template tags in my form, everything works fine.

Here is my register template:

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}Register{% endblock %}

{% block content %}
<h1>Sign up for a new account</h1>
<hr>
<form action="" method="post">
    {% csrf_token %}
    {{ form|crispy }}
    <button type="submit" class="btn btn-sm btn-primary">Register</button>
</form>
<p>
    Already have an account?
    <a href="#">Log in</a>
</p>
{% endblock %}

And just in case it is required, here is my view function:

def register(request):
    form = RegistrationForm()

    if request.method == 'POST':
        form = RegistrationForm(data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponse("Successfully Registered!!!")

    context = {
        'form':form,
    }
    return render(request, 'users/register.html',context)

I have specefied CRISPY_TEMPLATE_PACK in my settings.py and also added crispy_forms to my INSTALLED_APPS. Where am I going wrong?

EDIT:

So I did a bit more research on this topic and this error is probably because crispy forms does not support bootstrap 5 yet. So, by changing the bootstrap5 to bootstrap4 in my CRISPY_TEMPLATE_PACK, the error is solved


Solution

  • Based on the latest crispy form doc, it seems that there is no built-in bootstrap5 for it. Are you sure you are using bootstrap5?

    Currently, there are only bootstrap, bootstrap3, bootstrap4, and uni-form.

    You can take a look at your file structure if you even see bootstrap5 folder.