我只想滑动,但我不知道如何

时间:2020-09-23 05:19:04

标签: django django-views django-templates

自从几天以来,我试图找到合适的代码来在模板中滑动此图像,但是我无法实现,我需要您的帮助,让它燃烧。

模型1:

class MyFamily(models.Model):
    author = models.ForeignKey(Profile, on_delete=models.CASCADE)
    date = models.DateField(auto_now_add=True)
    update = models.DateField(auto_now=True)
    title = models.CharField(max_length=100)
    explanatipon = models.TextField(blank=True)
    photo1 = models.ImageField(upload_to='posts/photos/')
    photo2 = models.ImageField(upload_to='posts/photos/')
    photo3 = models.ImageField(upload_to='posts/photos/')
    photo4 = models.ImageField(upload_to='posts/photos/')
    photo5 = models.ImageField(upload_to='posts/photos/')
    active = models.BooleanField(default=True)

    @property
    def photos(self):
        photos = [self.photo1, self.photo2, self.photo3, self.photo4, self.photo5]
        return [photo for photo in photos if photo is not None]

    def __str__(self):
        return str(self.title)[:30]
    

个人资料:

class Profile(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return str(self.user.username)
    
    

观看次数:

def photo(request):
    myfamily = MyFamily.objects.filter(active=True)

    context = {
        'myfamily':myfamily
    }
    return render(request, 'posts/create-form.html', context)

模板:

<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    {% for photo in myfamily.photos %}
    <div class="carousel-item {% if forloop.first %}active{% endif %}">
      <img class="d-block w-100" src="{{ photo.url }}" alt="First slide">
    </div>
    {% endfor %}
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您的视图不返回上下文:

Enter the positive integer                                                                                                                                  
-10                                                                                                                                                         
Please enter a valid positive integer.                                                                                                                      
                                                                                                                                                            
                                                                                                                                                            
Enter the positive integer