How to Solve Error Message ‘crispy_form_tags’ is not a registered library in Django Web Application

Posted on

Introduction

This article will show how to solve an error message appear upon executing a Django-based web application. The error appear with the base application exist in the previous article. It is an article with the title of ‘How to Solve Error Message crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field’ in this link. So, all of the content is just fine as an example. The following is the actual directory structure of the project :

C:\programming\python\myproject>tree .
Folder PATH listing for volume Windows
Volume serial number is 00000027 E003:3593
├───myproject
│      asgi.py
│      forms.py
│      models.py
│      settings.py
│      urls.py
│      views.py
│      wsgi.py
│      __init__.py
└───templates
       contact.html

Apparently, upon executing the URL of the project which is the actual main URL of the project, the error above appear. The URL is ‘localhost:8000’. The definition of the URL exist in the ‘urls.py’ file. In that file, the following is the URL definition :

path('', views.contact),

In other words, upon accessing the URL of ‘localhost:8000’, it will directly execute the function of ‘contact’ exist in the ‘views.py’ file. The following is the actual content of the ‘views.py’ with the function of ‘contact’ as part of the content :

def contact(request):
    contactForm = ContactForm
    context = {'form': contactForm}
    render(request, 'templates/contact.html', context)

Last but not least, just check the content of the ‘contact.html’ exist in the ‘templates’ folder. The following is the content of the file :

{% load crispy_forms_tags %}
<!--Contact form-->
<div style="margin: 80px">
  <h1>Contact</h1>
  <h4>Contact us directly if you have any questions</h4>
  <p>
    Please write your name, email address and a message below if you have any
    questions. One of our staff members will be happy to contact you directly
    and answer your questions as soon as possible.
  </p>
  <form method="post">
    {% csrf_token %} {{ form.first_name|as_crispy_field }}
    <button type="submit">Submit</button>
  </form>
</div>

Solution

After searching further, apparently the error exist in the first line of the ‘contact.html’ file. It obvious since the error informing that ‘crispy_forms_tags’ is not a registered library. Since the first line is declaring a tag of ‘crispy_forms_tags’, in order to solve the problem, just register the tag. But how to register a tag ?. It is very simple. Since there is already a ‘django-crispy-forms library available, the only step is just to register the tag library. So, the steps are in the following sequence :

  1. First of all, check whether the crispy-forms library is available. Just execute the command ‘pip list’ as follows :

    (env) C:\programming\python\myproject>pip list
    Package Version
    ------------------- -------
    asgiref 3.4.1
    Django 3.2.7
    django-crispy-forms 1.12.0
    pip 21.2.4
    pytz 2021.1
    setuptools 41.2.0
    sqlparse 0.4.1
    
    (env) C:\programming\python\myproject>
    
  2. Next, check whether the crispy_forms library is exist and the registration is also already done. In order to do that, just check the settings.py file in the root folder of the project. The following is the actual content of the ‘settings.py’ in the part for registering a library or application :

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    

    It is obvious that there is no ‘crispy_forms’ in the application definition as a registered tag library. In order to define it and register it, just add in the last line the ‘crispy_forms’ entry. So, the content of the ‘settings.py’ file will be in the following display :

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'crispy_forms',
    ]
    
    How to Solve Error Message 'crispy_form_tags' is not a registered library in Django Web Application
    How to Solve Error Message ‘crispy_form_tags’ is not a registered library in Django Web Application

    After editing it and execute to access the main page further, at last it produce the success output as follows :

Leave a Reply