Facebook
From Ja, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 220
  1. #models.py
  2. class Gallery(models.Model):
  3.     title = models.CharField(max_length=256,null=True)
  4.     file = models.FileField(upload_to="files/%Y/%m/%d")
  5.     publish = models.DateTimeField(default=timezone.now)
  6.     slug = models.SlugField(unique_for_date='publish', max_length=250)
  7.  
  8.     def __str__(self):
  9.         return self.title
  10.  
  11.     def get_absolute_url(self):
  12.         return reverse('posts:gallery_detail', args=[
  13.             self.publish.year,
  14.             self.publish.strftime('%m'),
  15.             self.publish.strftime('%d'),
  16.             self.slug])
  17. #views.py
  18. def gallery_list(request):
  19.     image = Gallery.objects.all()
  20.     return render(request,'posts/gallery.html',{'image':image})
  21.  
  22. def gallery_detail(request,year,month,day,image):
  23.     images = get_object_or_404(Gallery.objects.filter(),slug = image,
  24.                                publish__year=year,
  25.                                publish__month=month,
  26.                              publish__day=day,)
  27.     return render(request,'posts/gallery_detail.html',{'images': images})
  28.  
  29. #galery_detail
  30. {% extends 'posts/post_base'%}
  31. {% block image %}
  32.     {% for image in images   %}
  33.      <div class="col-md-8">
  34.         <h1>{{ image.title }}</h1>
  35.          <img src="{{ image.file.url }}" alt="">
  36.        </div>
  37.     {% endfor %}
  38. {% endblock %}
  39.