How do I cache form data in Django? - python

I'm creating a single page application in Django.
It mainly consists of multiple, small forms that are filled in and validated independent from each other. Every form looks similar to this:
With a final button press at the end though, the input of all those forms should be processed together.
How can I save/cache all data of the submitted forms to process them with a final form submit at the end?
I tried creating only one big form, but then I can't validate each field one after the other, when the user clicks the 'Next' button.

If you want to have a form spread across multiple pages with multiple request/response cycles I'd probably recommend storing it either in the session or in the database. The session is probably easier whereas the database approach is more sophisticated. You can store items in the session using request.session['step_1'] = {} for example.

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.

How to provide different sets of data to different users in django?

I'm a newbie to the django framework and trying to make a watchlist for stocks. I've already made the crux of the webapp, where-in, a user can search for a quote and add it to their watchlist, along with relevant data about that quote.
What I want to do now is, to save the separate watchlists that different users are creating (after creating an account on my site) and upon logging in to my site, they can view their personalized watchlist and edit it.
I'm using a model for storing the data for the watchlist quotes and looking for a way to provide the different personalized watchlists depending upon the logged in user.
Can anyone give me a lead on how to employ the logic for this? Do I need to use two data bases - one for the data of the users and the other one for storing the respective user watchlists? If yes, how do I connect everything?
EDIT: Ever used a stock investment app? The way every user/customer can log in to their account and make/edit and save their watchlists in the app - that is the functionality I want to implement. How/Where do I store so many watchlists?
use 'request.user' from your view, to know the user who sent the request and return the corresponding watchlist

Django half filled forms

How do i save a half filled form?
eg: User logs in and registers for a service. Remembers that some scanned docs are missing. Saves the form for completion at a later date. Comes back completes it and submits it.
Solutions I came up with
I can set the required field to False and save the half filled form to DB. But that defeats the purpose of required fields.
I can save the half filled data in a seperate table. Migrate to the actual table once validating passes.
However for something so simple does Django not already have inbuilt functionalities.
If I am missing some term here that I should be googling point me in the right direction.
Django 1.6
Python 2.7
the easiest way to do this is to store the data posted and save it in the user session.
eg.
form_state = request.POST.copy()
request.session['form_state'] = form_state
Something similar is done by form form wizard (https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/)

Django and multi-image upload form

I have this form that includes around 25 inputs on the same. It includes a main image input and the rest are some text inputs or drop down menus.
The problem is that I also need the user to upload multiple images. I was thinking of doing it on the next page itself.
I have 2 questions:
What is the best way for adding this multiple image upload form to the current form? Not related to Django, more related to the structure of the form.
What is the best way of adding a multiple image/file to work correctly with Django? Any libraries or modules for such a job or any manual way to do it.
With formsets you allow the user to create several images at once. To create a formset out of an ImageForm you would do:
>>> from django.forms.formsets import formset_factory
>>> ImageFormSet = formset_factory(ImageForm)
https://docs.djangoproject.com/en/1.5/topics/forms/formsets/
And 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.
https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/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).

Categories