Django: How to request data and simultaneously have it editable - django

I have a simple app that gets user input and when a button is clicked, the input is saved as an entry on the database. I'm thinking of creating another app that not only displays the same information (think of view profile) but also simultaneously lets the user edit the text that is displayed in the text field.
I'm guessing the solution is to have the text-fields be auto-filled by pulling the data from the database, and allow overwriting the data once the submit button is clicked.

Typically read and edit views are separated for good reasons like avoiding accidental edits, allowing different levels of access, things like that. But this capability does exist in Django via forms. If you already have a Form built for submitting the data, you can provide a page with existing data pre-filled by initializing the Form instance with the data - in the docs they call this a bound form. See this example in the docs to get an idea of the mechanics.

Related

Is there a way to change the blank attribute on Django form fields to False by clicking a button?

I am building a review form that will post to a single model in my database. This form will have multiple sections, each with its own set of questions. I want the user to be able to complete a section and hit a submit button that will save the progress and redirect them to the next section. In essence I want to create an in progress status for the review with the idea that as all sections are finished the user can hit a complete button that will run my validation on the entire form. However, I do not want to allow posting information to the database that has not been cleaned.
I've been thinking through this trying to work out what I need to do and believe the best bet would be to have a complete button on the last page that changes blank to False for the fields of the form. I believe this would allow me to fill out each form and post it to the database as all fields would start as optional but then for that specific model instance the click of the button at the end would institute a change making all fields required, thereby running validators to ensure the form is complete. I believe this will also allow for saving progress and returning to it later if interrupted during completion of the form.
Can anyone provide any insight on if this is even possible? If so does anyone have examples that could guide me?
You can store the unvalidated data to the user's session. The final stage pulls all previous step data out of the session for validation. IIRC the Django form wizard does just this so you don't have to write it.
I might instead do this client-side, using one big (probably Crispy) form, and some client-side pseudo-submit buttons that just invoke Javascript to hide one section of the big form and reveal the next. The final one is a real submit, and redisplay of the whole thing if there are errors.

How to store a returned javascript value in a django form?

I want to store things like Browser type and GPS locations for mobile devices along with user input data.
I know how to set up a form in Django so that the user inputs are stored in the database. But how would I go about executing a Javascript function that will a return a value to be stored in the same model as all the user inputtext in the form?
I found reasonable two ways:
I would go for api and do request to server with information you want to store and put it to your model (for instance REST django-rest-framework). But its more complicated since you need to handle authorisation.
As altenative to api you could create hidden fields in form which would be filled by js code executed from the browser. When user clicks submit button he sends you data with no need to manually provide it.

Store temporary information in sitecore

I've a doubt about the best practices related with Sitecore.
I will need to store a form information when the user presses Save button while he's still filling all the form before submit. Therefore, in case he goes to the website in the next day he will see the already filled information and he can submit.Because the form is long and splitted in four steps.
My doubt is more related with the best practices for Sitecore. Where should I store this information?
Should I create a table inside sitecore_core or other existing database and read from there? (If there's any way to do that with Sitecore libs)
Or should I create my own database, probably with just two tables, to store that information?
Thanks
You should create your own database and store the information there. It is no best practice to add tables to the Sitecore databases and storing this information inside one of the existing tables (ie Sitecore items) is also not the way to go.
So just go for the custom database.
If you are using user profiles and (xdb) analytics data and want to store the form data there anyway, that could also be an option (using a custom facet).
You should use WFFM for saving any form information. In case if you want to send/save any additional information then you add your custom save action. Like for example we save data from the contact us form and newsletter form to Eloqua or Marketo for other purposes using custom action.

Django: Save user-submitted data from ModelForm for admin review and committing at a later date

I have a form on my Django site (made with ModelForm) where users can submit some data to create new objects or modify existing objects. These data, however, need to be reviewed by our staff before they're committed to the database, sometimes in bulk at a later date.
I know I can use .save(commit=False) in my form-processing view to return an object that has not been saved to the database yet. But is there a way to collect all of these objects from multiple user submissions for later review? Ideally, I'd have an admin page that had a summary list of submissions with "Commit" or "Reject" buttons.
There's no one-step-out-of-the-box way to do this (at least not built in to Django), so you'll need to create the logic yourself, which should be pretty straightforward. Some approaches to consider:
Have a second model/table to which your form saves, then create a view for the review functionality which copies any approved records into the first table.
Avoid having second model/table and add a approved = BoolField(default=False) field to your model/table. Set objects to a custom manager which filters for default=True so the rest of your code will only see approved records by default. Have a second manager that does the opposite, i.e. filtering for unapproved records. Using this second manager, create a view for the review functionality which flips approved to True for anything that gets approved.
If, with the second approach above, you want use Django's admin site to do the review, create a proxy for your model which by default uses the second manager which filters for unapproved records. Then you can use the admin's inline display and editing functionality to see records at a glance and click approve as needed.

django save a form for a user that has not registered yet

I need some advice / ideas if someone is inclined to help: I have a javascript interface for manipulating pictures. Basically it's about moving photos around. Once it's done the position of images is saved into a Django form and then saved to database with the owner saved as the current user. Now the trick is that I would like to be able to allow non registered users to play with this interface and then if they like the result they hit save and are redirected to an account registration page and only then the form is actually saved with their user as the owner.
What comes to my mind now is to keep the values of the form in session but I don't know what will happen to the session once the anonymous user registers and becomes another user. I was also thinking of using a 'next' parameter in the registration process with the url filled with get parameters that would be the content of the form but then I don't know if userena is ready to allow that.
Any light on this is welcome.
Well, we did similar thing on our site.
When unregistered user attach photos we save objects to database and assign unique hash which was generated when user came to the page with form. When user hit submit we pass this hash in url and on the next step, when user wants to register, we just get objects from database by this hash and assign user_id to them.
Also we have a cron job which do clean up and removes all lost objects
P.S. Sorry for my english i hope you'll get my point
Save the object without the user and store a reference of that object in the session (or (signed) cookie). If if the user registers, update all the objects with the newly created user.
Another approach would be to store the data in the browser (html5 localstorage and fallbacks, or similar) and only insert it into the database once the user has signed up. You would need to make sure both things happen inside the same browser 'instance', but things would be easier in the server side.