如何通过模板中的外键将模型与模型连接起来

时间:2015-05-09 08:48:23

标签: python django templates django-models

我有一个画廊(django 1.8中的应用程序),插入图像。我的问题是创建一个指向每个类别中的图像的链接。我不知道如何在模板一侧做到这一点,URL地址是正确的。

模型

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)
    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 GalleryView(generic.ListView):
    model = Paint
    template_name = "www/gallery_list.html"
    context_object_name = "gallery"

    def get_queryset(self):
        return Paint.objects.all()


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

网址

urlpatterns = [
    url(r'^$', MainPage.as_view(), name='mainpage'),
    url(r'^about/$', AboutMe.as_view(), name="about"),
    url(r'^gallery/$', GalleryView.as_view(), name="gallery"),
    url(r'^gallery1/$', GalleryDetailsView.as_view(), name="gallery_details"),
    url(r'^contact/$', contact, name="contact"),
]

模板库列表 - 主要部分

{% extends "base.html" %}
{% load thumbnail %}

{% block content %}


    {% for i in images %}
<div class="row">
    <div class="col-md-5">
        <img src="{{ i.image|thumbnail_url:'avatar1' }}"/>
    </div>

    <div class="col-md-7">
        <h3>{{ i.title }}</h3>
        <p>{{ i.description }}</p>
    </div>
</div>
<hr>
    {% endfor %}


{% endblock %}

{% block content_bottom %}{% endblock content_bottom %}

模板库细节与图库

{% extends "base.html" %}
{% load thumbnail %}

{% block content %}

    <table class="center-table">
        <tbody>
        {% for image in gallery %}
        {% if forloop.counter0|divisibleby:3 %}<tr>{% endif %}

        <td style="padding-left:50px;padding-right:50px;color:grey">
            <a data-lightbox="roadtrip" href="{{ image.paint.url }}"><img src="{{ image.paint|thumbnail_url:'avatar' }}" class="img-thumbnail img-responsive" /></a><br>
            <div style="padding-top:8px;padding-bottom:20px;">
                <b>Tytuł:</b> {{ image.title }}<br>
                <b>Status: </b>{{ image.status }}<br>
                <b>Cena: </b>{{ image.price }}<br>
            </div>
        </td>

        {% if forloop.counter|divisibleby:3 or forloop.last %}</tr>{% endif %}
        {% endfor %}
        </tbody>
    </table>
{% endblock %}

{% block content_bottom %}{% endblock content_bottom %}

0 个答案:

没有答案