0

大家好,我在这方面苦苦挣扎,谷歌搜索过这一硬核。

我正在开发一个图片库,该图片库输出了每个专辑,当单击屏幕叠加层时会显示图片库。已经在 javascript 中完成了在画廊中显示每个图像的艰苦工作,所以我只需要由以下给出的图像路径:

{{ images.content }}

以下是我在 django 项目中使用的文件。

这方面的帮助会很棒。

管理.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)

视图.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})

画廊.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>

我什么都没得到,我哪里出错了?

谢谢!

4

1 回答 1

1

gallery对象中没有调用属性Galleries

使用默认的反向 m2m 访问器images_set

{% for images in galleries.images_set.all %}

{% endfor %}
于 2013-01-16T00:37:20.483 回答