Kerst
Kerst

Reputation: 85

Django: passing multiple dictionaries to template isn't working

I'm new to Django and struggling with what I thought would be a simple task. I would like to pass a number of variables (dicts, query sets etc) to a template. I would like to access individual values from the all_q dictionary but none of the commands I try like: all_q.author or all_q.0.1 work. Is my view wrong or is the code wrong in my template? (the loop in the second div works fine)

View:

from django.shortcuts import render
from django.utils import timezone
from .models import Question

def home(request):
    questions = Question.objects.filter(created_date__lte=timezone.now()).order_by('created_date')
    return render(request, 'core/home.html', {'questions':questions, 'all_q':Question})

Template:

<div id "centreBlock">
    {{all_q.author}}
</div>
<div id = "rightBlock">
<h2> Other questions</h2>
    {% for quest in questions %}
    <h3><a href="">{{ quest.created_date }}</a></h3>
    <p>{{ quest.text|linebreaks }}</p>
    {% endfor %}
</div>

Upvotes: 0

Views: 1545

Answers (1)

djangoat
djangoat

Reputation: 485

You could access "indiviual values" like this:

   {{questions.0.author}}
   {{questions.1.author}}
   ....
   {{questions.42.author}}

etc. Question uninitilaized like that will not do what you want. Maybe Question.objects.all()?. It's really not clear what you are trying to do though.

Upvotes: 1

Related Questions