ways to avoid previous reload tornado - python

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.

Related

How to make Django Form repeatedly submit in background while user is filling it in?

Background information about my project:
I'm building a CV/Resume generator that automatically creates a CV/Resume based on the user filling out a form. I'm using a Django Crispy Form which has a submit button at the end that, when clicked, submits the user's input to a SQL database and redirects the user to their newly built CV/Resume (as a PDF).
What I need help with:
The goal is to have a form on the left side of the screen and a live view (HTML/CSS) of the CV/Resume on the right side, where the live view updates as the user is filling out the form.
I've seen this kind of thing before, but never for a Django project (they tend to use JavaScript/React).
What I'm thinking:
Could I have a background process that does something like, when the user makes a change (e.g. is filling out the form), submit any new inputs to the SQL database every 5 seconds? Then the live view can extract any new inputs from the database and display it in real time?
It might be possible to do something like that with ajax requests, but I wouldn't recommend it. It would be better to render a simulation of it in the browser with javascript html and css. That way you avoid many many needless post requests before the user is ready to submit the form.

Proper way to handle updating a view for Flask SPA using AJAX

This is a high level architectural question because I do not have web development experience, and I haven't found any succinct answers online.
Say I have a Flask app that starts off has a very simple html page with a few form fields. You key in some data into the form fields, submit, and the submission triggers an AJAX call onclick which then posts the results to your flask route.
When it comes time to display the data, you pull it out of the requests object which was posted to the relevant view, and then render a template passing through your formatted data.
My question is... if you want to continually make updates to the same page, how does that work? For example maybe after the first post hits your view you show a chart on the page with information passed to that view from the route. If you then decide to get a new graph for a different set of inputs to the form data, since you aren't hitting a new route do you just basically reload the page whenever the user keys in new data to the form fields and submits? How does this work if you want subsequent data to pop up after the chart? Do you just have multiple divs hidden/chilling in the background that unhide whenever certain events occur?
you usually use jquery to populate a div something like
<form id="my_form"> ... </form><button id="my_button">Clicky</button>
<div id="info_div"></div>
<script>
$("#my_button").click(function(){
var data = $("#my_form").serialize()
$("#info_div").load("/url/of/bit/to/load?"+data)
})</script>

Include post data from a second form when submitting

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.

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).

Pass variables to a "success" page after processing a form in Django

Is there anyway to pass context variables to a redirect response? I want to redirect a user to a success page after they submit a form, but I don't want the success page to be just a static html file. I need to display extra information based on the form data.
I have looked at this question, but the solution presented there simply renders a different file at the same url. I'd like to redirect the user so that hitting refresh at the page won't submit duplicate entries into the application.
Right now the only thing I have been able to use with some success is redirecting to a url while passing it GET variables as described here. That just seems like a bit of a hack, and was just wondering if there is any better solution...
Thank You
The way I see it you have three options:
Use GET variables in the redirect.
Store something in the session.
If you are creating an object using the form that was submitted, put the id of that object in the redirect url and use it in the new view.
The limitation you are running up against is that http is stateless, not something inherent in django.
How about storing your values in a session, then have the redirected page pick up the values from there?

Categories