Search code examples
pythondjangodjango-viewflow

How to render multiple forms in a single page using Material Design for django forms?


How can we display two forms in a single page using the Material Design for django forms?

In the forms.py, we can add code like

template = Template("""
    {% form %}
        {% part form.username prefix %}<i class="material-icons prefix">account_box</i>{% endpart %}
        {% part form.email prefix %}<i class="material-icons prefix">email</i>{% endpart %}
        {% part form.password prefix %}<i class="material-icons prefix">lock_open</i>{% endpart %}
    {% endform %}
    """)

And then in the template we have

{% block formbody %}
    {% include form.template %}
{% endblock %}

How we can add one more form to the page say form2?


Solution

  • This is explained in the documentation:

    Options:

    form - form instance to render, if not specified, takes from form context variable

    (emphasis mine)

    So to render a different form:

    {% form form=login_modal %}
         form code here
    {% endform %}
    

    This means that in your view, you need to provide the context variable bound to the form:

    from viewflow.flow.views import PerformTaskView
    from .forms import LoginForm
    
    class MyView(PerformTaskView):
        def get_context_data(self, **kwargs):
            context = super(MyView, self).get_context_data(**kwargs)
            context['login_modal'] = LoginForm()
            return context