我想计算用户上传文件的方式。 我添加了signal.py
from django.dispatch import Signal
upload_completed = Signal(providing_args=['upload'])
和summary.py
from django.dispatch import receiver
from .signals import upload_completed
@receiver(charge_completed)
def increment_total_uploads(sender, total, **kwargs):
total_u += total
到我的项目。
我的视图上传
@login_required
def upload(request):
# Handle file upload
user = request.user
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.uploaded_by = request.user.profile
upload_completed.send(sender=self.__class__, 'upload')
#send signal to summary
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('upload'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the upload page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(request,'upload.html',{'documents': documents, 'form': form})
这种努力没有用。我知道了
upload_completed.send(sender=self.__class__, 'upload')
^
SyntaxError: positional argument follows keyword argument
我找到了信号示例testing-django-signals
from .signals import charge_completed
@classmethod
def process_charge(cls, total):
# Process charge…
if success:
charge_completed.send_robust(
sender=cls,
total=total,
)
但是在我看来,类方法在我的情况下不起作用
如何解决我的方法?
答案 0 :(得分:1)
send()方法不需要'uploads'参数。
但是,如果您打算持续统计文件上载的数量(我认为您很可能会这样),那么我认为您应该创建一个新模型,以便可以将其保存在数据库中然后,您可以在每次保存文档模型时更新该模型。
我建议您看看post_save。祝您编码愉快!