Count a file download

Submitted 3 years, 8 months ago
Ticket #56
Views 242
Language/Framework Django
Priority Urgent
Status Closed

I want to make something like when someone download a pdf from my site after downloading download value will be increasing     

Submitted on Jul 31, 20
add a comment

2 Answers

I think you can use this approach

  1. Create the model to track the count of download by user or session basis 
  2. Then create mixin for your view
  3. If client request the url pattern then inherit this mixin in your view
  4. In mixin you can create the object with counter of download and save it in database.

Submitted 3 years, 8 months ago

Below are sample mixin you can use in your Class based view to inherit the functionality.

from .signals import object_viewed_signal


class ObjectViewedMixin(object):
    def get_context_data(self, *args, **kwargs):
        context = super(ObjectViewedMixin, self).get_context_data(*args, **kwargs)
        request = self.request
        instance  = context.get('object')
        if instance:
            object_viewed_signal.send(instance.__class__, instance=instance, request=request)
        return context

Submitted 3 years, 8 months ago

@Js-Tutul can you response whether the issue has been resolved. If resolved kindly close the ticket.

- Vengat 3 years, 8 months ago


Latest Blogs