Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass variable in to template in django

Can anyone help me to solve my Problem, I’m trying to pass a variable from my view to my Template. But the variable does not show up when I load my the site.

Model:

class Artikl(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
category = models.CharField(max_length=60)
price = models.PositiveIntegerField()
author = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
    return self.name

Views:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from django.shortcuts import render
from .models import Artikl

def home(request):

    context = {
        'artikl': Artikl.objects.all()
    }

    return render(request, 'base/home2.html', context)

Template:

    {% for artikl in Artikl %}
    <div class="row g-4">
        <div class="col-md-6 col-lg-3">
            <div class="card bg-light">
                <div class="card-body text-center">
                    <img src="https://picsum.photos/300/200" class="mb-3 img-fluid" alt="">
                
                    <h3 class="card-title mb-3">{{ artikl.name }}</h3>
                    <p class="card-text">{{ artikl.description }}</p>
                    
                    <button type="button" class="btn btn-primary">Kupi</button>
                </div>
            </div>
        </div> 
        {%endfor%}

>Solution :

Try this,

in template, you’re not referring to the context you’re passing

from django.shortcuts import render
from .models import Artikl

def home(request):

    context = {
        'Artikl': Artikl.objects.all()
    }

    return render(request, 'base/home2.html', context)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading