How can I create a button for a django admin action? - django

I have a django admin action say "refresh", I want to add a refresh button for each row in the admin list view. I can create button using format_html but how can I invoke "refresh" action when it is pressed?

I don't think admin actions are the best fit in this case.
Actions are
simple functions that get called with a list of objects selected on the change list page
With the buttons you describe however, you want to operate on a single row/instance.
Therefore I would simply create a custom url endpoint for your ModelAdmin which is called when you press the button and which handles the desired action.
This article has quite a comprehensive overview how this can be done in detail.

Related

Update model field in Django with button click on a template

I am new to Django and I am having some troubles with my first web application.
My idea is to show a template on user first login with some information about the web. The template contains an "accept button" and a "continue to the webpage" button. This template should appear everytime the users login, until they click on the accept button.
I have created a new model with userID, a boolean field (terms_accepted) and a date field (terms_accepted_date) to control if the users have accepted the information, and the date when they have done so. What I want is to update the terms_accepted (from False to True), when the users click on the accept button.
Is there a simply way to modify the model from the template? I have created the correct flow, because If I modify manually the terms_accepted field from the admin site, when I enter to the page I don't see the template with the information. However, I don't know how to update the field automatically on button click.
Thanks
please take a look at this tutorial:
https://docs.djangoproject.com/en/1.10/intro/tutorial04/.
In the example the form is more complicated, but you can see how the model is updated in polls/view.py

How can I hide a Siebel Applet button for a specific set of Views?

I would like to hide the Query button in an Applet, but only for a specific set of Views.
Is there a recommended way to do this? I'm looking for a flexible and easy to configure solution.
Even if you hide the query button, user will still be able to query using Alt-Q button on the applet. Do you want to prevent this as well ? Then you will have ot hide the button AND disable the query method as well.
In openui, you may be able to hide the query button using javascript. Or you could clone the applet, remove the query button, and show it in those different views. This is how some of us used to do this before OpenUI.
To disable the query, you will have to set CANINVOKE to false using applet user properties or applet scripting.

Adding an action to the change record screen

I read through the tutorial on the Django site for adding custom admin actions and got my custom action to work when viewing a list of items.
The problem is I'd like to add a button to the page where users can edit records that will execute this custom action, but I have no idea how to do this.

Adding extra functionality to Django Admin

It's a fairly simple idea, but I'm not sure where I should start reading.
All I want to do is add an extra action to a model in the Django Admin.
For example, I have a basic client management system, in that I have models for Clients, Companies, and Invoices to keep track of who owes what. All I want to do is add an action or button that when used, pulls information from the Invoices, Clients, and possibly Companies and then puts that information into a PDF document.
Any suggestions?
You'll have to override your admin templates for each of your models to add a button => Overriding Admin Templates
To generate a PDF define a view that is called once your button is pressed. Make sure to set the right permissions for your admin users.

django POST [Legitimacy]

Lets say I have a web application that displays 2 random buttons on each session, and I want the user to click on 1 button or the other. If a button is clicked, I want to increment the score for that button. However, the user has no control on what 2 buttons are presented each time and there are thousands of possible buttons.
If I create a view to increment the button, how would I be sure that the user wont just manually go to that view through the browser.
My thoughts are that you need to create a form and have users POST to a certain address, but how do you know that the user didn't just send a post request through AJAX to increment the button and that the user actually received that button from the randomized session with 2 buttons.
How would I handle this in Django is there documentation detailing how Django specifically supports this?
EDIT: Or do you have to create a unique session ID for each button selecting session and check each button click against the unique session ID.
Create a session variable to track the button they see, like:
def button_display_view(request):
# ...lots of cool logic
session['buttons_shown'] = [button_1_primary_key, button_2_primary_key]
return response
Then, in your button click view, ensure that the button clicked was in the list of buttons that they were shown. The user will have zero knowledge of your session variable, nor be able to edit it, much like he/she can't when you use session variables to track authentication, etc.
If you're building a multivariate testing system (or even an AB testing system), it might be worth checking out this MVT app for ideas.