Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Pass multiple objects to templates with render

I'm beginner on Django.

I have a project with the following models:

My Articles models:

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()

My Portfolio categories models which is linked with my Article Model:

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

And finally, my portfolio models with all the photos:

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')

In one view and one template, i'd like to display information concerning the article and the portfolio related to the article.

I wrote the following view:

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})

And the following templates:

<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>

I can access to information from my article but i can't access information from my portfolio. I tried it with the shell, and it works. I can't figure out why it doesn't work in my view and template.

Do you have any idea?

Thank you in advance

Singertwist

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Try this:

# 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})

Leave your HTML as is.

about 4 years ago · Santiago Trujillo Report

0

Your image_portfolio is a querySet, that's means is some kind of list, you have to use a loop to access the items and them access the properties:

<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 %}
about 4 years ago · Santiago Trujillo Report

0

Hi thank you all for your answer.

So, I used a for loop for solving my case as mentioned previously.

Below, my code:

  <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>

And my views:

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})

Thanks again for your help

Singertwist

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!