Include post data from a second form when submitting - python

I am working on a django template that includes two forms. Lets call the first one main and the second one sub. I want to be able to submit main and get the post data from sub with it. Sub contains options for main and I need to be able to change main and re-select similar options from sub.
I have everything set up to the point that I can get the post data from main but I can't figure out how to include sub. Any help would be appreciated.
Thanks,

Django comes with an optional “form wizard” application that splits forms across multiple Web pages. It maintains state in one of the backends so that the full server-side processing can be delayed until the submission of the final form.
Read the official docs here: Form Wizard.

Related

How to have single form on multiple pages

I am looking for ways to best handle a single form on multiple pages. Like how a newsletter signup might be on a home page, an about page, and several blog pages.
I've handled this exact scenario in these 2 ways.
Write an API (using DRF) that takes a POST request, and then point the normal HTML form at that API. This works, and is very flexible, but feels like overkill.
Pass the form object into the context of each view I want the form to be on, and then include that into the template with includes form_snippet with form=form
The first approach is more flexible with wagtail, wherein all that needs to happen on the admin side is an inclusion of the newsletter as a snippet, where the admin user is able to choose if they want the newsletter on the page or not.
While both approaches work just fine, neither of them "feels" right, as I would think there is a simple way to do this without creating a big API, or passing around the form object to every single view.
three years later on you probably found an answer.
For documentation purposes and Google searchers, Wagtail offers an explanation on how to make a multiple step form here:
https://docs.wagtail.org/en/v3.0.1/reference/contrib/forms/customisation.html#multi-step-form
I did this with a contact formular. I handled it with a new app for my contact formular.
In this contactApp is the templates/contactForm.html. To include this contactForm where I want I use {% include 'contact/contactForm.html' %} so it loads the contactForm.html from my app called contact.

ways to avoid previous reload tornado

I have two forms, when I submit form#1 I get some corresponding file, but when I submit form#2 thenafter, the corresponding file gets shown but form#1 goes empty. So basically I want some thing like a SPA(e.g angular) but I am taking form#1 and form#2 as separate requests routes and each render my index.html every time, so form#2 is wiped off when I submit form#1 and vice-versa.
I dont want a working code but any ideas on how I do that with Tornado (not angular, or say Tornado + Angular ? )
I think one way for example is to handle these requests via a controller and do an AJAX post to corresponding Tornado Handler, which after the file is rendered, displays / serves the very file back again. But this uses AngularJS as a SPA. Any other solution possible?
Thanks in Advance
This is not really a Tornado question, as this is simply how Web works.
One possible solution is to have only one form, but display its fields so that they look like two forms; in addition, have two separate submit buttons, each with its own name and value. Now, when you click on either button the whole form will be submitted, but in the handler you can process only the fields associated with the clicked button, while still displaying values in all the fields.

Creating reusable forms/views in Flask

I am not sure what would be the best route to go down, or I may be missing something obvious.
Example I can give is I have 'person' model and associated form, and view created to add a new 'person'. This all works great. What I would like to do though is use this 'view/form' in a master page with other similar 'views/forms'. With each part being able to add/edit or delete a record from each sub view/form.
So I have all functionality done, just don't know how I can create this master page with child objects, but these child objects can be their own page as well, type of thing.
The idea being that the master page structure is flexible and can accommodate various elements based on the context the user is in.
Should I be looking at blueprints or Jinja2 and its template structure. Or is it how I am handling routes within the main app.
Apologies if this is too vague.
I have done this using AngularJS ng-include directive. You can include whatever html you want, but be careful with Jinja2. If the html you are trying to include contains any script tag it will crash. See my question here. If you need to import a form that needs a script tag you will need to make sure it is not loaded when you are pushing it with Angular. Since it makes a xhr request, you can use flask.request.is_xhr to check if it is angular or the user that is requiring the form. You cannot forget to add this to your angular app
Otherwise is_xhr will always return false.
myAppModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
Be careful with base_ templates as well, since they usually load script tags. You can pass the base template through your flask route, and make it extend a blank base html when the request is made through angular. I wish I had my example here, but I am on the bus. Let me know how far you got.

Submitting Multiple Forms At The Same Time (Edit Profile Page)

My question I suppose is rather simple. Basically, I have a profile. It has many variables being passed in. For instance, name, username, profile picture, and many others that are updated by their own respective pages. So one page would be used to update the profile picture, and that form would submit data from the form to the handler, and put() it to the database. What i'm trying to do here, is put all of the forms used to edit the profile on one single page at the same time.
Would I need one huge handler to deal with that page? When I hit 'save' at the bottom of the page, how do I avoid overwriting data that hasn't been modified? Currently, say I have 5 profile variables, they map to 5 handlers, and 5 separate pages that contain their own respective form.
Thanks.
I've used django on most of my webapps, but the concept should be the same; I use ajax to send the data to the backend whenever the user hits submit (and the form returns false) so the user can keep editing it. With ajax, you can send the data to different handlers on the backend. Also, using jQuery, you can set flags to see if fields have been changed, to avoid sending the ajax message in the first place. Ajax requests behave almost exactly like standard HTTP requests, but I believe the header indicates AJAX.
If you're looking at strictly backend, then you will need to do multiple "if" statements on the backend and check one field at a time to see if it has been changed. On the backend you should still be able to call other handlers (passing them the same request).

Django FormPreview - What is it for?

While looking across the Django documentation, I came across the FormPreview.
The description says this:
Django comes with an optional “form preview” application that helps automate the following workflow:
“Display an HTML form, force a preview, then do something with the submission.”
What is meant by "force a preview"? What would you use this feature for an in application?
To force a preview means the users are forced to see the value they have inserted on the form input fields, before django actually saves it to the database.
One example is django comment system, which enforce the users to take a look at the comment they have written before django actually saves it to the database. You would see that the users are redirected to another page to take a look at their comment, and after that there is a submit button to actually save the comment.
I think they mean (I use django but I didn't know of this until now..) that you can let people write, for example in a textarea box like I'm doing right now. After the user submits it the system would preview it to the user and give him the chance to read and edit what he submitted, before it being submitted again all the way to the database.

Categories