1

We need to render forms in python and capture the html output. 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. Any thoughts?

2
  • do you have a helper? do you have a layout ? what is exactly the use case? Why do you need HTML in Python if I can ask, there might be better ways to customize output. Commented Jun 28, 2012 at 10:42
  • I do have a helper and a layout. The use case is twofold. 1) I want to test form html output. 2) I want to have a form contain a subform. So, in python, I want to render the subform down to html, and then inject that html into the container form where the subform-field is located. Commented Jun 28, 2012 at 19:50

1 Answer 1

4

These are the lines in the node for the {% crispy %} tag for rendering a form with a helper + layout. 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?

EDIT 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.