[Answer]-Why do my CBV forms have no fields?

1👍

Try remove redundant ‘context’ in your html, {{ context.form1 }} would incorrectly links to context['context']['form1'] which doesn’t exist.

<form action="" method="post">
    {% csrf_token %}
    {{ form1.as_p }}
    <input type="submit">
</form>

<form action="" method="post">
    {% csrf_token %}
    {{ form2.as_p }}
    <input type="submit">
</form>

0👍

  1. Remove the useless context in your template, just form1.as_p is fine.

    <form action="" method="post">
        {% csrf_token %}
        {{ form1.as_p }}
        <input type="submit">
    </form>
    
    <form action="" method="post">
        {% csrf_token %}
        {{ form2.as_p }}
        <input type="submit">
    </form>
    
  2. You need to instantiate a form object and not passing the class in your context. Here, you are passing a class, and Django won’t be able to use it.

    class MultipleFormView(TemplateView):
        template_name = 'blog/multiple_form.html'
    
        def get_context_data(self, **kwargs):
            context = super(MultipleFormView, self).get_context_data(**kwargs)
            context['form1'] = Form1()  # <----
            context['form2'] = Form2()  # note the parenthesis here
            return context
    
            success_url = '/'
    

Leave a comment