如何使用 Material Design for django forms 在单个页面中呈现多个表单?

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

我们如何使用 Material Design for django forms 在一个页面中显示两个表单?

在forms.py中,我们可以添加这样的代码

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 %}
    """)

然后在模板中我们有

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

我们如何向页面添加一个表单,比如 form2?

这在the documentation中有解释:

Options:

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

(强调我的)

所以要呈现不同的形式:

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

这意味着在您看来,您需要提供绑定到表单的上下文变量:

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