使用 jinja2 制作 django 脆皮形式 [英] django crispy forms with jinja2

查看:21
本文介绍了使用 jinja2 制作 django 脆皮形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时使用 Jinja2 和 Django 脆皮表单,但是当我在模板中加载脆皮表单标签并尝试使用此模板标签呈现表单时,出现错误:

I want to use Jinja2 and Django crispy forms together, but when I load crispy_forms_tags in my template and trying to render form with this template tag, I get an error:

Encountered unknown tag 'crispy'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.

我的模板代码:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="panel-heading"><h3>Registration</h3></div>
    {% crispy form %}
{% endblock %}

如何一起使用这些应用程序?

How can I use this apps together?

推荐答案

更好的解决方案:

模板标签.py

from crispy_forms.utils import render_crispy_form
from django_jinja import library
from jinja2 import contextfunction


@contextfunction
@library.global_function
def crispy(context, form):
    return render_crispy_form(form, context=context)

在模板中:

{{ crispy(form) }}

这篇关于使用 jinja2 制作 django 脆皮形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆