djangomultiple-variable-return

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


Solution

  • 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 %}