0

There is a lot of similar question. But my problem is slightly different.

In a view, I am using multiple forms, for that, I am using formset and normal form.here is my view:

class CreateInvoice(TemplateView):
template_name = 'product/create_invoice.html'
product_list = Product.objects.get_all_product()
ItemFormSet = formset_factory(ItemForm, formset=BaseItemFormSet, extra=0)

def get_context_data(self, **kwargs):
    if 'items' not in kwargs:
        kwargs['items'] = self.ItemFormSet()

    if 'order' not in kwargs:
        kwargs['order'] = OrderForm()

    kwargs['product_list'] = self.product_list

    return super().get_context_data(**kwargs)

def post(self, request):
    item_formset = self.ItemFormSet(request.POST)
    order_form = OrderForm(request.POST)
    if order_form.is_valid() and item_formset.is_valid():
        order = order_form.cleaned_data
        items = item_formset.cleaned_data
        order['sold_by'] = request.user
        order_id = Order.objects.crate_new_order(order=order, items=items)
        return HttpResponseRedirect(self.render_to_response(self.get_context_data(pos_invoice=generate_pos_invoice(), order_id=order_id)))
    else:
        return HttpResponseRedirect(self.render_to_response(self.get_context_data(items=item_formset, order=order_form,)))

If forms are not valid then I need to render the same page with error messages. If forms are valid then also need to render the same page with the HTML receipt string and the extra data.

But here the problem is if the browser is refreshed (like pres crtl+f5) then the same forms are submitted again. From googling I came to know that this problem is created for I am using render instead of a redirect. Here is the link of the question from where I came to know this: Django form submit again on refresh

Now the main problem If I am redirecting the page then I am losing the functionality that I want to have. Like, can't send error messages if forms are invalid and send extra data like receipt HTML page string if forms are valid.

Now I don't know how to solve this issue or suggest a better approach for doing that. Thanks in advance.

Almabud
  • 152
  • 2
  • 9
  • Do you mean like when you refresh & the browser prompts with an `ok/cancel` dialog to resubmit the form? I wouldn't worry about browser behaviour like that, if people want to do that then it's up to them. You can just setup necessary validation so duplicate objects etc can't cause you problems because a refresh would be the same as the form just being filled in again. – markwalker_ Apr 14 '20 at 15:12
  • I just want just load the page simply without resubmitting the form whenever the refresh button of the browser is clicked. – Almabud Apr 14 '20 at 15:17
  • I don't know if it can help, I was figured out how it can be managed using a "MainClass" which show the templates, datas, forms, etc... and several "SubClass" which will make the job. Then, in "SubClass", use a redirection to the "MainClass". "To send error message", place some control directly in your forms (def_clean, etc...), witch can be done too. – Bea Apr 14 '20 at 15:20
  • If I used redirect then my old error message will be void. And If I am not using redirect then the form is submitting whenever the browser is refreshed. – Almabud Apr 14 '20 at 15:24

0 Answers0