Home > Software design >  related_name foreign key doesn't show on template django
related_name foreign key doesn't show on template django

Time:10-21

i have 2 models and i use related_name for one of them :

memory model:

class Memory(models.Model):
memory_title=models.CharField(max_length=200,verbose_name='عنوان خاطره')
memory_text=models.TextField(verbose_name='متن خاطره')
register_date=models.DateField(default=timezone.now,verbose_name='زمان ثبت خاطره')
is_active=models.BooleanField(default=False,verbose_name='فعال/غیرفال')
user_registered=models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='کاربر ثبت کننده')

memory gallery model:

class MemoryGalley(models.Model):
memory_image_name= models.ImageField(upload_to=upload_gallery_image, verbose_name='تصویر خاطره')
memory=models.ForeignKey(Memory,on_delete=models.CASCADE,null=True,related_name='pics')

and fuction for upload image:

def upload_gallery_image(instance,filename):
return f"images/memory/{instance.memory.memory_title}/gallery/{filename}"

views.py:

class ShowmMemries(View):
def get(self,request,*args, **kwargs):
    memories=Memory.objects.filter(is_active=True)
    if request.user.is_authenticated:
        list_memory_liked=MemoryLike.objects.filter(user_liked_id=request.user.id).values("memory_id")
        list_memory_liked_id=[memory["memory_id"] for memory in list_memory_liked]
        return render(request,'MemoriseFerdowsApp/showmemory.html',context={'memories':memories,'list_memory_liked_id':list_memory_liked_id})
    return render(request,'MemoriseFerdowsApp/showmemory.html',context={'memories':memories})

and html file :

          {% for image in memory.pics.all %}

          <div >
            <img src="{{image.memory_image_name.url}}"  alt="...">
          </div>
        
      {% endfor %}

my problem is all images doesn't show in html files.

in html file

but in inspect there are address of images.

how can i fix this problem ?

CodePudding user response:

I may be missing something but it looks like you may just need to make one of your carousel-item's active. From the bootstrap carousel docs:

Initial active element required The .active class needs to be added to one of the slides. Otherwise, the carousel will not be visible.

https://getbootstrap.com/docs/5.0/components/carousel/#example

  • Related