【问题标题】:How to resolve Django crispy-forms Template Syntax Error如何解决 Django 脆表单模板语法错误
【发布时间】:2020-07-24 18:40:51
【问题描述】:

嘿,我正在尝试学习 Django 脆形式,所以当我尝试放入/注入 {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')
    )

我该如何解决这个错误?

【问题讨论】:

    标签: python html django django-crispy-forms


    【解决方案1】:

    我也遇到了这个问题,服务器运行正常,然后当我修改 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

    【讨论】:

      【解决方案2】:

      那是因为您没有在设置中的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 中添加这些清晰的标签应该可以正常工作。

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

      你可以在here看到更多

      【讨论】:

      • 我已经在安装的应用程序中添加了crispy_forms,所以我想这不是问题
      • 为此你做了pip install django-crispy-forms?
      • 是的,在我使用酥脆的表格之前
      • @x_CrazyTeddy_x Omg,刚刚注意到业余错误,它是 crispy_forms_tags 而不是 crispy_form_tags。加载后者,它应该可以解决您的问题。如果对您的问题有帮助,也请接受/支持答案
      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2015-01-07
      • 2021-06-25
      • 2019-07-24
      相关资源
      最近更新 更多