外键在djang中不起作用

2024-04-20 03:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在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 %}

Tags: nameselfverbosetitlemodelsdef画廊gallery