大家好,我在这个问题上苦苦挣扎,谷歌搜索了这个硬核。
我正在开发一个图库,其中每个相册都会输出,当点击屏幕叠加时会显示图库。在javascript中已经完成了艰苦的工作,用于在库中显示每个图像,因此我只需要由以下内容给出的图像路径:
{{ images.content }}
以下是我在django项目中使用的文件。
这方面的帮助很棒。
manage.py
from django.db import models
PHOTO_PATH = 'media_gallery'
class Gallerys(models.Model):
title = models.CharField(max_length=30, help_text='Title of the image maximum 30 characters.')
slug = models.SlugField(unique_for_date='date', help_text='This is automatic, used in the URL.')
date = models.DateTimeField()
class Meta:
verbose_name_plural = "Image Galleries"
ordering = ('-date',)
def __unicode__(self):
return self.title
class Images(models.Model):
title = models.CharField(max_length=30, help_text='Title of the image maximum 30 characters.')
content = models.FileField(upload_to=PHOTO_PATH,blank=False, help_text='Ensure the image size is small and it\'s aspect ratio is 16:9.')
gallery = models.ManyToManyField(Gallerys)
date = models.DateTimeField()
class Meta:
verbose_name_plural = "Images"
ordering = ('-date',)
def __unicode__(self):
return self.title
import models
from django.contrib import admin
class ImagesAdmin(admin.ModelAdmin):
list_display = ('title', 'date')
class GallerysAdmin(admin.ModelAdmin):
list_display = ('title', 'date', 'slug')
admin.site.register(models.Images, ImagesAdmin)
admin.site.register(models.Gallerys,GallerysAdmin)
views.py
from django.http import HttpResponse
from notices.models import Notices
from django.shortcuts import get_object_or_404
from django.shortcuts import render_to_response
from gallery.models import *
from navigation.models import *
# when the galleries page is requested, all image galleries are listed by date created with the latest first,
# each gallery displayed contains a javascript tag containing an index of images separated by ';'
def galleries(request):
gallery = Gallerys.objects.all()
images = Images.objects.all()
return render_to_response('galleries.html', {'Galleries': gallery, 'Images': images})
galleries.html
{% for galleries in Galleries %}
<h1>{{ galleries.title }}</h1>
{% for images in galleries.gallery.all %}
<h2>{{ images.content }}</h2>
{% empty %}
none
{% endfor %}
{% endfor %}
当我迭代时:
<h2>{{ images.content }}</h2>
我什么都没得到,我哪里错了?
谢谢!
答案 0 :(得分:1)
gallery
对象中没有名为Galleries
的属性。
使用默认的反向m2m访问者images_set
{% for images in galleries.images_set.all %}
{% endfor %}