Django: Run scripts from admin by selecting object's name

时间:2015-09-01 22:35:32

标签: python django python-2.7 web admin

I am using Django 1.8 and Python 2.7.8 to create a web interface with MySQL databases, I can just use the Admin page to manipulate different tables. Suppose I have a student object with fields: name, id and gender. How to achieve some function like this:

  • select student's name: just avoid typo in names
  • click a button like "Run script"
  • trigger a Python program to run

I only need output from that Python program like: success or failure.

Thanks very much!

1 个答案:

答案 0 :(得分:1)

您所追求的是管理员行动。您在管理员中编写自定义函数,并将函数名称分配给admin_actions属性。

def incomplete_tasks(modeladmin, request, queryset):
    queryset.update(completed=False)
incomplete_tasks.short_description = 'Mark as Not Complete'

class TaskAdmin(admin.ModelAdmin):
    list_display = ['title', 'completed']
    ordering = ['created']
    actions = [incomplete_tasks,]

admin.site.register(Task, TaskAdmin)

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/

https://godjango.com/78-custom-django-admin-actions/