L
L
lunrox2014-02-07 13:44:38
Django
lunrox, 2014-02-07 13:44:38

How to create multiple forms on one page in Django?

Hello. I'm having a problem rendering multiple forms on the same page in a Django project.
I have a product that needs to be displayed on a page using a DetailView and the same page needs to have two forms that allow two different objects to be created.
So far, here's what I got, but it doesn't work:
Views.py:

class ProductDetails(DetailView, CreateView):

    model = Product
    template_name = 'product/product_detail.html'
    form_class = PhoneCompanyMessageForm
    second_form_class = TextCompanyMessageForm
    success_url = '/'
    mess_type = True #if true, then Text, else Phone message

    def get_queryset(self):
        qs = super(ProductDetails, self).get_queryset()
        return qs.select_related("categories")

    def get_context_data(self, **kwargs):
        context = super(ProductDetails, self).get_context_data(**kwargs)
        if 'form' not in context:
            context['form'] = self.form_class()
        if 'form2' not in context:
            context['form2'] = self.second_form_class()
        return context

    def form_invalid(self, **kwargs):
        return self.render_to_response(self.get_context_data(**kwargs))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if 'form' in request.POST:
            form_class = self.get_form_class()
            form_name = 'form'
        else:
            form_class = self.second_form_class
            form_name = 'form2'
            self.mess_type = False

        form = self.get_form(form_class)

        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(**{form_name: form})

    def form_valid(self, form):
        instance = form.save(commit=False)
        instance.company = self.object.company
        instance.product = self.object
        instance.mess_type = self.mess_type
        instance.save()
        return HttpResponseRedirect('/')

forms.py:
class PhoneCompanyMessageForm(forms.ModelForm):
    class Meta:
        model = ProductCompanyMessage
        fields = ['sender_name',
                  'phone',
                  ]

    def __init__(self, *args, **kwargs):
        super(PhoneCompanyMessageForm, self).__init__(*args, **kwargs)
        for key in self.fields:
            self.fields[key].required = True

class TextCompanyMessageForm(forms.ModelForm):
    class Meta:
        model = ProductCompanyMessage
        fields = ['sender_name',
                  'text',
                  'email',
                  ]

    def __init__(self, *args, **kwargs):
        super(TextCompanyMessageForm, self).__init__(*args, **kwargs)
        for key in self.fields:
            self.fields[key].required = True

template:
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="form" value="Submit" />
</form>

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

The product is displayed, when you click submit on the forms, a redirect is made to a page without content. /products/ page, even though all redirects lead to '/'
Using Django 1.6
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alternativshik, 2014-02-07
@alternativshik

The first thing that caught my eye is that forms can use the prefix parameter just for cases when you need several forms on 1 page.
def form_invalid(self, **kwargs):
return self.render_to_response(self.get_context_data(**kwargs))
apparently, hence the problem. Is the form invalid?

S
silentboy13, 2014-02-07
@silentboy13

Use formset to display multiple forms https://docs.djangoproject.com/en/1.6/topics/forms...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question