Django 自定义管理操作自动更新/刷新管理页面
我有一个管理操作,可以从我的数据库中打开一个 pdf 对象并更新与该行关联的某些字段。一旦执行操作,如何让管理页面自动显示对这些字段的更改,就像预安装的删除管理操作所发生的那样?我已经尝试使用 HttpResponseRedirect 作为解决方法,但无法使其与我的响应对象结合使用 - 只有一个或另一个工作。有没有简单的方法让页面自动刷新?预先感谢您的任何指导!
from django.contrib import admin
from django.contrib.auth.models import User
from djangostuff.pdf.models import ABC
from django.http import HttpResponse, HttpResponseRedirect
import datetime, time
class ABCAdmin(admin.ModelAdmin):
actions = ['print_selected_pdf']
def get_user(self):
return '%s'%(self.user.username)
def create_pdf(self, request, queryset):
response = HttpResponse(mimetype="application/pdf")
response['Content-Disposition'] = 'attachment; filename=form.pdf'
for obj in queryset:
response.write(obj.form)
rows_updated = ABC.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
if rows_updated == 1:
message_bit = "1 form was"
else:
message_bit = "%s forms were" % rows_updated
self.message_user(request, "%s successfully printed." % message_bit)
return response
#HttpResponseRedirect("/admin/pdf/abc")
def print_selected_pdf(self, request, queryset):
# prints the pdfs for those that are selected,
# regardless if the pdf_printed field is true or false
qs = queryset.filter(pdf_printed__exact=0)
return self.create_pdf(request, qs)
print_selected_pdf.short_description = "Print Selected PDF"
get_user.short_description='Printed By'
list_display=('form_no',get_user,'request_date','request_time','pdf_printed')
admin.site.register(ABC, ABCAdmin)
I have a an admin action that opens a pdf object from my db and updates certain fields associated with that row. How would I get the admin page to automatically display the changes to those fields as happens with the pre-installed delete admin action once the action is executed? I have experimented with using a HttpResponseRedirect as a work around but have not been able to get that to work in conjunction with my reponse object - only one or the other work. Is there a simple method for getting the page to auto refresh? Thanks in advance for any guidance!
from django.contrib import admin
from django.contrib.auth.models import User
from djangostuff.pdf.models import ABC
from django.http import HttpResponse, HttpResponseRedirect
import datetime, time
class ABCAdmin(admin.ModelAdmin):
actions = ['print_selected_pdf']
def get_user(self):
return '%s'%(self.user.username)
def create_pdf(self, request, queryset):
response = HttpResponse(mimetype="application/pdf")
response['Content-Disposition'] = 'attachment; filename=form.pdf'
for obj in queryset:
response.write(obj.form)
rows_updated = ABC.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
if rows_updated == 1:
message_bit = "1 form was"
else:
message_bit = "%s forms were" % rows_updated
self.message_user(request, "%s successfully printed." % message_bit)
return response
#HttpResponseRedirect("/admin/pdf/abc")
def print_selected_pdf(self, request, queryset):
# prints the pdfs for those that are selected,
# regardless if the pdf_printed field is true or false
qs = queryset.filter(pdf_printed__exact=0)
return self.create_pdf(request, qs)
print_selected_pdf.short_description = "Print Selected PDF"
get_user.short_description='Printed By'
list_display=('form_no',get_user,'request_date','request_time','pdf_printed')
admin.site.register(ABC, ABCAdmin)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要解决此问题,您可以创建一个中间页面来重定向以进行确认,然后将用户重定向回原始更改列表页面,然后将显示您的消息。用于删除对象的内置管理操作已经实现了这一点。
您可以在这里阅读更多相关信息:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages
To solve this issue, you can create an intermediary page to redirect for confirmation, then redirect the user back to the original change list page and your message will be displayed. The built in admin action for deleting an object already accomplishes this.
You can read more about it here:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages