django model attribute edit & save with no extra page - django

I rende certain objects values in django template in a form of a table.
I let user to edit the value and save the edit so I can track the history of edit.
At the moment I use django forms to let user do single object attribute value OR chosen objects attribute values OR all of them and save it.
My problem is with forms is that the way it works at the moment is:
user clicks a value in 'main' page so it links to object 'edit' page in which I return a form so user can edit it.
The problem is with that extra url or extra page. I do not want to do it via separate pages.
I would like to click on the object (like in the excel) and change the value there in 'main' page and submit the edits from the same page.
How can I achieve it with django ?
Can somebody point me into right direction and point out what I should read about to understand it or how I should do it ?
I want to edit either single or let user edit multiple objects values and save the changes and still be able to track the history of edits / changes.

You should look into modals. So when a user clicks to edit it will display a pop-up that you can render the form in without leaving the page.

Related

Django admin button for fields to trigger action

In the Django admin change form of my model I would like to be able to add a button next to any field. Clicking this button should call some function with the current form data as input. Once the function returns, the Django admin form should be updated by the return values of the function.
I know of one way to add a button at the very bottom of the admin model change pape, right below the save button, but this is not what I want. The button should be right next to the specific input field.
Currently, I have no idea where to start and conceptionally how this is achieved.
I googled a lot, but couldn't find a solution yet. I thought of creating a new form widget that I could assign to an inputfield, but buttons in forms are not available in django.

Saving ModelForm progress values to session in Django

I have flow where users can create (model) forms. If form is valid, object gets saved and flow continues, but selection is in multiple pages. I need to keep current state of created object, which cannot be saved before it's completely valid.
One thing I can do is to always pass things around those views in the ModelForm to make sure, that user never loses data, but I also wanna make sure, that if he leaves the flow and comes back, he doesn't lose data, that he already entered previously.
That's why I decided I wanna save all the fields to session.
Is this correct approach?
How would you do this?
Where would you put this session logic?
What's best way of getting the fields from incomplete form to be saved?
Edit:
Please don't give me advice on how to use session, I am talking more about high level logic and architecture than specific implementation.
I should describe my flow a bit more. Model has 3 fields.
normal dropdown (foreign key referencing another model)
textfield
another foreign key, but this time not done by select, but it's own separate page with lots of filters to help user pick the right (foreign) model
Flow is not linear, because user can start in different parts of page.
Sometimes user can go to page, where he has first 2 fields + button "Browse", which takes you to selection page for 3rd field. Then after he selects field there, he comes back.
But sometimes he selects first this field and then comes to screen with 2 remaining fields, where he needs to fill those.
django-formtools offers a great way to do this using Form wizard.
The form wizard application 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.
More info here https://django-formtools.readthedocs.io/en/latest/wizard.html
to save in session:
request.session["variable_name"] = "value"
to get from session request.session["variable_name"]. sure you can use request.session.get("..") in both too

After saving in a form i want it to point to another site not the forms list

It's probably a stupid question.
I have a form on a custom list that I hade to custom design (the form for editing). But because of the law (GDPR) I can't let the users see the list after they save the form.
One part is sloved by them entering in the form from a page with a direct link to it. But the relinking is not.
This is the original part that I figured I have to change
<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton2"/>
I changed It to this
<SharePoint:SaveButton RedirectUrl="http://www.google.com" runat="server" ControlMode="New" id="savebutton2"/>
This also hellped but It just retuns me to the same empty form.
Can you tell me what I have to change to get it to actualy link it to the redirect link?
As I understand your question, you want to redirect users to another site after they've submitted a form.
What I have done in the past is modify the URL of the new form so that instead of the new item url being : http://mysite/org/hr/Lists/Exit/Item/newifs.aspx?List=b5b9e317%2D4366%2D4557%2D8d29%2Db5dedc71a75a&Source=http%3A%2F%2Fmysite%2Forg%2Fhr%2FLists%2FExit%2FCompleted%2Easpx&RootFolder=&Web=a86f3198%2D79a6%2D4e7f%2Daa26%2D448559533df8
I change the address after &Source in the above URL to the other site. So, it would now look like this:
http://mysite/org/hr/Lists/Exit/Item/newifs.aspx?List=b5b9e317%2D4366%2D4557%2D8d29%2Db5dedc71a75a&Source=http://www.google.com
What will happen, form will open, they'll save, and once the form closes, it will direct them to Google. I didn't need to modify the OOB save buttons - it's a good deal cleaner and quicker!
Hope this helps.

Simple dynamic form in Django controlled by the user

I want to make a simple accounting application and I have a model with 3 fields:
account_symbol
account_debit
account_credit
And I want to make a form for the user to enter the accounts for the balance, after entering the first set (of 3 fields) the form (after the user press a button) should reveal another set and so on.
After all the accounts should be entered, the user should submit the form to the database.
I read the b-list.org approach and I don't think is what I need and the formsets docs but what I want is the user to control the length of the form by adding additional sets of 3 fields.
Please point me to an approach for my problem.
Formset is exactly what you need here. You just need to use JavaScript to dynamically show new formsets to user (like in admin app). Here's nice example of doing this.

Django: after submiting POST data page opens at a given div id, not at the top of the page

I have a quite long and complicated form which has several submit buttons. When the user clicks a submit button their data is saved but then they are sent back to the top of the page, which is annoying because the user then has to scroll back down to get to the part of the form they were working on.
(The form controls the color and layout of the user's webpage, so the expectation is that the user will make many changes, submitting many times, rather than just filling out the form and submitting once.)
Is there any way to send the user to the correct part of the page (preferably without resorting to Javascript)?
Create an anchor in your page at the correct location:
<a name="section1">Section 1</a>
Then use Django's HttpResponseRedirect and with the anchor specified.
return HttpResponseRedirect('/form#section1')
Haven't tested this myself, but I'm assuming it will work because I've seen a few snippets like this one.