O
O
Olya2020-01-09 04:07:53
Django
Olya, 2020-01-09 04:07:53

What is the best way to organize many different forms on a page?

Hello!
There was a problem of placement of set of forms on different pages. Each form is part of a separate application (app) with its own paths. Almost all applications are their own, some are third-party.
It seems to be an obvious solution - to use inclusion-tags , but, unfortunately, I did not find good implementation examples, and a lot of questions arose, two main ones:
1. How to write a tag correctly?
If all processing is placed in the view, then only the form render remains for the tag:

@register.inclusion_tag('core/my/form.html')
def my_form():
    return {'form': MyForm()}
but in this case it is not clear how incorrectly filled fields in the form will be highlighted, because it will be rendered empty every time.
If all the logic is transferred to a tag, then how to connect it to the paths and how to distinguish a post request from different forms - analyze the path or enter a hidden name for the form?
2. How to return the user back to the page from the form handler? For example the user is on the page '/index' and the form handler has the path '/core/subscribe'. When the button is clicked in the form, the view should go to '/core/subscribe', and then return the user to '/index' with a message about a successful subscription or with an invalid form and incorrectly filled fields highlighted.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2020-01-09
@leahch

The idea to give forms through rendering is not very correct at all. This was true 10 years ago, where there were not a lot of forms on the page, and one form - one view worked perfectly. I recommend sending and passing forms via json/ajax, and for rendering use the client side and javascript with jquery/reactjs/vuejs/angularjs... Yes, this probably crosses out your inner world a little, but it just so happened that today it is so .
On the other hand, everything becomes simpler - write templates with your own bunch of forms, write endpoint routes for ajax with json receiving and issuing, the rest is client side. And it’s easier to test, and to keep a bunch of forms on one page, and it’s also easier to send them, in the end it will even work faster, since you don’t need to drive the entire page every time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question