如何解决 Django crispy-forms 模板语法错误

How to resolve Django crispy-forms Template Syntax Error

嘿,我正在尝试学习 Django 脆皮形式,所以当我尝试 putting/injecting {load crispy_forms_tags %} 时,我得到以下错误

错误信息

TemplateSyntaxError at /login/

'crispy_form_tags' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
crispy_forms_field
crispy_forms_filters
crispy_forms_tags
crispy_forms_utils
i18n
l10n
log
static
tz

代码如下:HTML

{% load crispy_form_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0   /css/bootstrap.min.css"  integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"  crossorigin="anonymous">
    <title>Login</title>
</head>
<body>

    {% crispy form  %}


</body>
</html>

models.py

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField


class Accounts(models.Model):

    name = models.CharField(max_length=100, null=False, blank=False)
    last_name = models.CharField(max_length=100,null=False, blank=False)
    phone = PhoneNumberField(null=False, blank=False, unique=True)
    email = models.EmailField(null=False, blank=False, unique=True)
    password = models.CharField(null=False, blank=False, max_length=100)
    verify_password = models.CharField(null=False, blank=False, max_length=100)


    def __str__(self):
        return self.name

forms.py

class UserAccount(forms.Form):

class Meta:
    terms = (

        ('agree', 'Agree'),
        ('disagree', 'Disagree')
    )
    model = Accounts
    password = forms.CharField(widget=forms.PasswordInput())
    verify_password = forms.CharField(widget=forms.PasswordInput())
    terms_and_conditions = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=terms) 
    fields = (

        'name',
        'last_name',
        'phone',
        'email',
        'password',
        'verify_password',
        'terms_and_conditions'
    )


def __init__(self, *args, **kwargs):
    super().__init__(self, *args, **kwargs)

    self.helper = FormHelper
    self.helper.form_method = 'POST'

    self.helper.layout = (

        'name', 
        'last_name',
        'phone', 
        'email', 
        'password',
        'verify_password',
        'terms_and_conditions',
        Submit('submit', 'Submit', css_class='btn-success')
    )

我该如何解决这个错误?

那是因为您还没有在设置的INSTALLED_APPS中添加Crispy Form tag。在那里添加这个 'crispy_forms' 应该可以解决问题,例如

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.staticfiles',
    'crispy_forms',
]

在您的 templates.html 中添加这些 crisp 标签应该可以正常工作。

{% load crispy_forms_tags %}
  {% csrf_token %}
{% crispy form %}

你可以从here

看到更多

我也遇到过这个问题,服务器 运行 正常,然后当我修改 app_tags 时,我遇到了同样的问题。

Django-crispy-forms 是使用 pip install django-crispy-forms 安装的。 crispy_forms 已添加到 settings.py INSTALLED_APPS,但问题仍然存在。

因此,应用所有节省,重新启动您的服务器,如果问题仍然存在,请重新启动您的数据库,然后 IDE。这为我解决了。

https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/#code-layout