Django admin button for fields to trigger action - django

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.

Related

Django admin on adding a new FK show in modal instead of a popup

Hello I am wondering how to make the add_form to show in modal instead of a popup when adding a new foreign key instance of a field in the django admin.
Explanation.
When pressing the + button on FK I want the form in a modal instead of a popup
This isn't currently possible with django admin. You could implement it yourself, but you would need to:
1) write the code for the modal with the form that you want yourself, you'd also need to handle
2) Then extend the original template to open the modal, handle the requests etc, update or reload the original page.
Tbh, it would be far more effort than it's worth for something cosmetic like a modal. I'm sorry that this probably isn't the answer you're after, but it really won't be worth the hours that it would require to make this happen :(
If it's really critical that you need a modal for this, you probably shouldn't be using the admin site at all (it's not designed to be a production ready site).

django model attribute edit & save with no extra page

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.

Can I add a 'shortcut' link to a Django form which edits a field and saves everything?

I have a Django (crispy) form with many tabs which all contain many fields. Each of these tabs has an Active boolean field. The first tab of my the form contains an overview, listing all the tabs.
Now I would like to know if there is some way to have a link on that overview tab which would in theory toggle the corresponding boolean field on one of the tabs and save the result. Right now the user can go to the specific tab, check or uncheck the value and save it, but they want some sort of shortcut so they can do it in one click.
Would that be possible at all? And how could I achieve this ?
So clicking the Activate / Deactivate in this list should be the same as :
Going to the corresponding tab and checking / unchecking the available field and pressing the save button.
You can use javascript and do onchange for checkbox. In the script you can call the views to save the value to database and return to same page.

how to display my own custome message in the top of admin page

want to display my own custom message and link in the top of localhost/admin/module/field before the select fields to change page. I tried with link_display that for displaying each instance obj itself, and i don't want that.
You need to override the change_list.html template for your app, see the documentation.
Money quote for your problem:
For example, if we wanted to add a tool to the change list view for
all the models in an app named my_app, we would copy
contrib/admin/templates/admin/change_list.html to the
templates/admin/my_app/ directory of our project, and make any
necessary changes.

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.