外键在django中不起作用

时间:2015-05-10 07:14:43

标签: python django django-models django-queryset

我在Django中有一个应用程序,我在其中绘制图库并将链接的图片添加到图库中,但是在模板的一侧没有出现任何内容。

模型

class Gallery(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    image = ThumbnailerImageField(upload_to='paint/%Y/%m/%d')

    class Meta:
        verbose_name = "Gallery"
        verbose_name_plural = " Galleries"

    def __unicode__(self):
        return self.title


class Paint(models.Model):
    AVAILABLE = "Available"
    NOT_AVAILABLE = "Not available"
    STATUS_PAINT = (
        (AVAILABLE, u"Dostępny"),
        (NOT_AVAILABLE, u"Nie dostępny")
    )
    title = models.CharField(max_length=200)
    gallery = models.ForeignKey(Gallery, related_name='paint_set')
    paint = ThumbnailerImageField(upload_to='paint/%Y/%m/%d')
    price = models.CharField(max_length=50, blank=True, null=True)
    status = models.CharField(choices=STATUS_PAINT, default=AVAILABLE, max_length=50)

    class Meta:
        verbose_name = "Picture"
        verbose_name_plural = "Images"

    def __unicode__(self):
        return self.title

视图

class GalleryList(generic.ListView):
    model = Gallery
    context_object_name = "images"
    template_name = "www/gallery_list.html"


class GalleryDetailsView1(generic.DetailView):
    model = Gallery
    context_object_name = "images1"
    template_name = "www/gallery_details1.html"

    def get_object(self):
        return Gallery.objects.get(pk=self.kwargs['pk']).paint_set.all()

网址

这是我的网址:图库列表以及链接到图库

的画家的详细信息
url(r'^gallery/$', GalleryList.as_view(), name="gallery_list"),
url(r'^gallery1/(?P<pk>\d+)/$', GalleryDetailsView1.as_view(), name="gallery_details1"),

模板详情

{% for i in images1 %}

{{ i.title }}

{% endfor %}


{% endblock %}

0 个答案:

没有答案