Hed*_*ide 14
更新:只需call_command('compilemessages')在python代码中的任何位置调用该函数,即可运行任何管理命令.
例:
from django.core.management import call_command
call_command('compilemessages')
Run Code Online (Sandbox Code Playgroud)
如果任务绑定到当前在管理员中查看的对象,那么一种很好的方法可能是在单击按钮时实现ajax脚本调用的额外视图.额外的视图可以可选地包装为芹菜任务,例如
models.py
class Foo(models.Model):
# fields...
def my_task_init(self):
return mark_safe("<img class='loading' src='/static/img/loading.gif' alt='loading' style='display:none;' /><a data-identifier='task_%i' class='task'><img src='/static/img/process.png' style='cursor:pointer;' /></a>") % self.id
my_task_init.allow_tags = True
my_task_init.short_description = _(u"Execute Task")
Run Code Online (Sandbox Code Playgroud)
admin.py
class FooAdmin(admin.ModelAdmin):
list_display = ['other_field', 'my_task_init']
class Media:
js = (
'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js',
'/static/js/admin_tasks.js',
)
def get_urls(self):
urls = super(FooAdmin, self).get_urls()
extra_urls = patterns('',
(r'^my-task/$', self.admin_site.admin_view(self.parse_view))
)
return extra_urls + urls
# optionally decorated by celery
def task_view(self, request):
if not request.is_ajax():
raise Http404
task_id = request.GET.get('task_id')
# your logic
return HttpResponse('Success')
Run Code Online (Sandbox Code Playgroud)
admin_tasks.js
$(document).ready(function (){
$('.task').click(function(){
var image = $(this).find('img'),
loading = $(this).parent().find('.loading'),
task_id = $(this).data('identifier').replace('task_', '');
$.ajax({
type: "GET",
data: ({'task_id': task_id}),
url: "/admin/app/model/my-task/",
beforeSend: function() {
image.hide();
loading.show();
},
statusCode: {
200: function() {
loading.hide();
image.attr('src', '/static/img/success.png');
image.show();
},
404: function() {
loading.hide();
image.attr('src', '/static/img/error.png');
image.show();
},
500: function() {
loading.hide();
image.attr('src', '/static/img/error.png');
image.show();
}
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
如果您尝试启动未绑定任务,则可以覆盖模板元素或添加一些html.