0

I am developing a web-site using Django/Python. I am quite new to this technology and I want to do the web-site in a right way. So here is my problem:

Imagine, that there is a Product entity and product view to display the Product info. I use (product_view in my views.py ).

There is also Message entity and the Product might have multiple of them. In Product view page ( I use "product_view" action in my views.py ) I also query for the messages and display them.

Now, there should be a form to submit a new message ( in product view page ).

Question #1: what action name should form have ( Django way, I do understand I might assign whatever action I want )?

Option #1: it might be the same action "product_view". In product_view logic I might check for the HTTP method ( get or post ) and handle form submit or just get request. But it feels a bit controversial for me to submit a message to the "product_view" action.

Option #2: create an action named "product_view_message_save". ( I don't want to create just "message_save", because there might be multiple ways to submit a message ). So I handle the logic there and then I make a redirect to product_view. Now the fun part is: if the form is invalid, I try to put this form to the session, make the redirect to the "product_view", get the form there and display an error near the message field. However, the form in Django is not serializable. I can find a workaround, but it just doesn't feel right again.

What would you say? Any help/advice would be highly appreciated!

Best Regards, Maksim

2
  • By "action" do you mean a "view function"? Are the "messages" something similar to having "comments" to an article or a product description page? Commented Dec 11, 2015 at 0:20
  • Yes. Users will leave a message like a comments to an article Commented Dec 11, 2015 at 11:51

1 Answer 1

1

You could use either option.

Option #1: In the post method (if using Class-based-views, otherwise check for "post" as the request type), just instantiate the form with MessageForm(request.POST), and then check the form's is_valid() method. If the form is valid, save the Message object and redirect back to the same view using HttpResponseRedirect within the if form.is_valid(): code block.

If you're checking for the related Messages objects in your template, the newly created message should be there.

Option #2: Very similar to Option #1, except if the form is not valid, re-render the same template that is used for the product_view with the non-valid form instance included in the template context.

Sign up to request clarification or add additional context in comments.

3 Comments

The question is not in the code level ( I know how to create a form from the post request, how to validate it etc ). The question is more in "approach" level or in "structure" level. As always in the programming, there are multiple ways to do it. Multiple workarounds to do it. The question is: what is considered the best practice in Django? Option #1? Option #2? Or something other. It's not about the code.
By the way, as I described for Option #2, I would like to make a redirect after form submit and form is not serializable ( I've already found for a workaround how to serialize a form ), but this doesn't seem to be a good solution.
I'm not sure there is a "right way" that's recommended by the Django project, but in my opinion, if you only have one form that is performing a post on the Product page, it's easy enough to just keep it within the same view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.