使用 render 将多个对象传递给模板

Pass multiple objects to templates with render

我是 Django 的初学者。

我有一个包含以下模型的项目:

我的文章模型:

class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=160)
    content = models.TextField()
    image = models.ImageField(upload_to=upload_location)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    categorie = models.CharField(max_length=200)
    categorie = models.ForeignKey('categorie.Categorie')
    publier = models.BooleanField()

与我的文章模型链接的我的投资组合类别模型:

class Categorieport(models.Model):
    title = models.CharField(max_length=200)
    article = models.OneToOneField('posts.Post')    

最后,我的作品集模型包含所有照片:

class Portfolio(models.Model):
    title = models.CharField(max_length=200)
    image = models.ImageField(upload_to=upload_location)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    categorieportfolio = models.ForeignKey('Categorieport')

在一个视图和一个模板中,我想显示有关文章和与文章相关的投资组合的信息。

我写了以下视图:

def portfolio(request, article=None):
    portfolio = get_object_or_404(Categorieport, article=article)
    image_portfolio = portfolio.portfolio_set.all()
    return render(request, 'portfolio1.html', {'portfolio': portfolio, 'image_portfolio': image_portfolio})

以及以下模板:

<div class="titre-display">
    <h2>{{ portfolio.article.timestamp|date:"d E Y" }} / {{ portfolio.article.categorie}} </h2>
    <h1>{{ portfolio.article.title}}</h1>
</div>  

<div class="content-display">
    <div class="content-display-main">
        <div class="content-display-main-portfolio">
            <div class="image-portfolio">
                <a class="example-image-link" href="{{ image_portfolio.image.url }}" data-lightbox="example-set" data-title="{{image_portfolio.title}}">
                </a>

我可以访问我的文章中的信息,但我无法访问我的投资组合中的信息。我用 shell 试了一下,效果很好。我不明白为什么它在我的视图和模板中不起作用。

你有什么想法吗?

提前致谢

Singertwist

试试这个:

# views.py

def portfolio(request, article=None):
    # first get the Post instance with slug = article (I'm assuming article passed as url argument, is a slug)
    post = get_object_or_404(Post, slug=article)

    # get the Categoriepost object based on a specifi article
    categorie_port = get_object_or_404(Categorieport, article=post)

    # image_portfolio is a QuerySet (that is a list of Portfolio objects)
    image_portfolio = categorie_port.portfolio_set.all()

    return render(request, 'portfolio1.html', {'portfolio': categorie_port, 'image_portfolio': image_portfolio})

保持你的 HTML 不变。

你的 image_portfolio 是一个 querySet,这意味着是某种列表,你必须使用循环来访问项目并访问属性:

<div class="content-display">
    <div class="content-display-main">
        <div class="content-display-main-portfolio">
            <div class="image-portfolio">
           {% for item_img in image_portfolio %}     
                <a class="example-image-link" href="{{ item_img.image.url }}" data-lightbox="example-set" data-title="{{item_img.title}}"></a>
            {% endfor %}

嗨,谢谢大家的回答。

因此,如前所述,我使用 for 循环来解决我的案例。

下面是我的代码:

  <div class="titre-display">
        <h2>{{ portfolio.article.timestamp|date:"d E Y" }} / {{ portfolio.article.categorie}} </h2>
        <h1>{{ portfolio.article.title}}</h1>
    </div>  

    <div class="content-display">

        <div class="content-display-main">

        <div class="content-display-main-portfolio">


              {% for photo in image_portfolio %}  

              <div class="image-portfolio">

              <a class="example-image-link" href="{{ photo.image.url }}" data-lightbox="example-set" data-title="{{photo.title}}">

            {% thumbnail photo.image "300x300" crop="center" as im %}
                <img class="example-image" src="{{ im.url }}" alt=""/>
            {% endthumbnail %}    

              </a>
              <p>{{photo.title}}</p>
              </div>

            {% empty %}

                <p>Aucun portfolio.</p>

            {% endfor %}          

        </div>            

        </div>

以及我的看法:

def portfolio(request, slug=None, article=None):
slug = get_object_or_404(Post, slug=slug)
portfolio = get_object_or_404(Categorieport, article=article)
image_portfolio = portfolio.portfolio_set.all()
return render(request, 'portfolio.html', {'portfolio': portfolio, 'image_portfolio': image_portfolio})

再次感谢您的帮助

Singertwist