4

I would like to write a script that is not activated by a certain URL, but by clicking on a link from the admin interface. How do I do this? Thanks!

asked Mar 4, 2009 at 0:17
1
  • Clicking a link on browser side doesn't cause server to do anything, unless it is an hyperlink (ie. <a href="">). Please edit your question to clarify: do you just want to restirict this action to a subset of users or you want to stay in the same page after clicking? Commented Mar 4, 2009 at 20:18

1 Answer 1

11

But a link has to go to a URL, so I think what you mean is you want to have a view function that is only visible in the admin interface, and that view function runs a script?

If so, override admin/base_site.html template with something this simple:

{% extends "admin/base.html" %}
{% block nav-global %}
 <p><a href="{% url your-named-url %}">Do Something</a></p>
{% endblock %}

This (should) put the link at the top of the admin interface.

Add your url with named pattern to your urls.py

Then just make a normal django view and at the top of the view check to make sure the user is superuser like this:

if not request.user.is_staff:
 return Http404

That will prevent unauthorized people from accessing this view.

Next, in your view after the above code, just run the script.

Do that with Python's subprocess module, for example:

from subprocess import call
retcode = call(["/full/path/myscript.py", "arg1"])
answered Mar 4, 2009 at 0:31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.