title: How to track download link in django?
body: <p>I followed these method to track how much downloaded the file was. But <code>total_downloads</code> always remains same (it's 0). How to increment <code>total_downloads</code> field by 1 after every download?</p>
  <p>My <strong>models.py</strong>:</p>
  <pre><code>from django.db import models
  
  class FilesAdmin(models.Model):
      id_no = models.IntegerField()
      name = models.CharField(max_length=20)
      loc = models.CharField(max_length=20)
      adminupload = models.FileField(upload_to='media')
      total_downloads = models.IntegerField(default=0)
  
      def __str__(self):
          return self.name
  </code></pre>
  <p><strong>views.py</strong>. In this program, I want to increment the number of downloads. But it's 0 in admin site.</p>
  <pre><code>from django.shortcuts import render
  from django.http import HttpResponse
  import os
  from .models import FilesAdmin
  
  def index(request):
      context = {'file': FilesAdmin.objects.all()}
      return render(request,'libooki/index.html',context)
  
  
  def download(request,path,pk):
      file_path = os.path.join(settings.MEDIA_ROOT,path)
      if os.path.exists(file_path):
          with open(file_path,'rb') as fh:
              response = HttpResponse(fh.read(),content_type=&quot;application/adminupload&quot;)
              response['Content-Disposition']='inline;filename'+os.path.basename(file_path)
              n = FilesAdmin.objects.get(pk=pk)
              n.total_downloads += 1
              n.save()
              return response
  </code></pre>
  <p><strong>urls.py</strong></p>
  <pre><code>from django.contrib import admin
  from django.urls import include,path
  from django.conf import settings
  from django.conf.urls.static import static
  from libooki import views #here's libooki is my app name
  from django.conf.urls import url
  from django.views.static import serve
  
  urlpatterns = [
      path('', views.index,name='index'),
      path('admin/', admin.site.urls),
      url(r'^download/(?P&lt;path&gt;.*)$',serve,{'document_root': settings.MEDIA_ROOT}),
  ]
  if settings.DEBUG:
      urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
      urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  <p><strong>index.html</strong> from where people can download the file</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
        &lt;title&gt;hello&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      {% for post in file%}
          &lt;h2&gt;{{post.name}}&lt;/h2&gt;
          &lt;a href=&quot;{{post.adminupload.url}}&quot; download=&quot;{{post.adminupload.url}}&quot;&gt;Download&lt;/a&gt;
  
      {% endfor %}
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>