Django 模板报错经典:crispy_forms_tags
模板经典报错:crispy_forms_tags
职位管理 - 创建职位页面报错TemplateSyntaxError at /accounts/positions/create/
'crispy_forms_tags' is not a registered tag library. Must be one of:
用户组管理 - 创建用户组页面报错:TemplateSyntaxError at /accounts/groups/create/
'crispy_forms_tags' is not a registered tag library. Must be one of:
原因和解决办法
原因
这通常是由两个原因之一造成的:
django-crispy-forms 这个库没有被安装。
crispy_forms 这个应用没有被注册到项目的 INSTALLED_APPS 设置中。
解决办法
第一步:安装插件:pip install django-crispy-forms
➜ baking_wms git:(v19) ✗ pip install django-crispy-forms
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Read timed out. (read timeout=15)")': /pypi/simple/django-crispy-forms/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Read timed out. (read timeout=15)")': /pypi/simple/django-crispy-forms/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Read timed out. (read timeout=15)")': /pypi/simple/django-crispy-forms/
^CERROR: Operation cancelled by user
[notice] A new release of pip is available: 24.0 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
➜ baking_wms git:(v19) ✗ pip install django-crispy-forms -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting django-crispy-forms
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1d/ec/a25f81e56a674e63cf6c3dd8e36b1b3fecc238fecd6098504adc0cc61402/django_crispy_forms-2.4-py3-none-any.whl (31 kB)
Requirement already satisfied: django>=4.2 in ./venv/lib/python3.11/site-packages (from django-crispy-forms) (5.2.3)
Requirement already satisfied: asgiref>=3.8.1 in ./venv/lib/python3.11/site-packages (from django>=4.2->django-crispy-forms) (3.8.1)
Requirement already satisfied: sqlparse>=0.3.1 in ./venv/lib/python3.11/site-packages (from django>=4.2->django-crispy-forms) (0.5.3)
Installing collected packages: django-crispy-forms
Successfully installed django-crispy-forms-2.4
[notice] A new release of pip is available: 24.0 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
➜ baking_wms git:(v19)
第二步:安装模板包
安装模板包(可选):
如果你使用 Bootstrap,需要额外安装对应的 crispy 模板包:
pip install crispy-bootstrap5
对应 Bootstrap5
第三步:在项目settings.py中注册
// ... existing code ...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
// ... existing code ...
'django.contrib.staticfiles',
# 第三方应用
'rest_framework',
'django_filters',
'crispy_forms',
'crispy_bootstrap5',
# 自定义应用
'core.apps.CoreConfig',
'accounts.apps.AccountsConfig',
// ... existing code ...
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
'EXCEPTION_HANDLER': 'baking_wms.exceptions.custom_exception_handler',
}
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"