2011-08-18 126 views
2

我有一個管理操作,從我的數據庫中打開一個pdf對象,並更新與該行關聯的某些字段。我將如何讓管理頁面在執行操作後自動顯示對這些字段的更改,就像預安裝的刪除管理操作一樣?我已經嘗試過使用HttpResponseRedirect作爲解決方法,但一直未能與我的響應對象一起工作 - 只有一個或另一個工作。有沒有簡單的方法讓頁面自動刷新?預先感謝任何指導!Django自定義管理操作自動更新/刷新管理頁面

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) 
+0

見問題http://stackoverflow.com/questions/987669/tying-in-to-django-admins-model-history – BenH

回答