简体   繁体   English

在python中通过django-crispy-forms渲染表单

[英]Rendering forms through django-crispy-forms in python

We need to render forms in python and capture the html output. 我们需要在python中渲染表单并捕获html输出。 So far we're resorting to a dirty hack: 到目前为止,我们正在采取一个肮脏的黑客:

def crispy_page(form):                
    mini_template = """                          
        {% load crispy_forms_tags %}                 
        {% crispy form %}                            
    """                                          
    from django.template import Template, Context
    t = Template(mini_template)                  
    c = Context({"form":form})                   
    return t.render(c)

We'd like to just be able to get the rendered output directly from crispy forms somehow, without having to hop through the django templating system. 我们希望能够以某种方式直接从脆弱的形式获得渲染输出,而不必跳过django模板系统。 Any thoughts? 有什么想法吗?

These are the lines in the node for the {% crispy %} tag for rendering a form with a helper + layout. 这些是 {% crispy %}标记的节点中的行 ,用于呈现带有帮助器+布局的表单。 I could refactor those out, so that they are easy to be called from outside. 我可以重构那些,这样他们很容易从外面打电话。 The application wasn't thought to be used server side, that's why it's so rigid for that. 该应用程序不被认为是使用服务器端,这就是为什么它如此严格。

It's my understanding that you have some fields of a form A, then fields of another form B and then more fields of form A, is that right? 我的理解是你有一些形式A的字段,然后是另一种形式B的字段,然后是形式A的更多字段,是吗?

EDIT 2012/7/30 编辑2012/7/30

Version 1.2.0 will come with a render_crispy_form function that solves your issue: https://github.com/maraujop/django-crispy-forms/issues/64#issuecomment-7354910 版本1.2.0将附带一个render_crispy_form函数来解决您的问题: https//github.com/maraujop/django-crispy-forms/issues/64#issuecomment-7354910

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM