Django:将多个词典传递给模板不起作用
Django: passing multiple dictionaries to template isn't working
我是 Django 的新手,正在努力完成我认为是一项简单的任务。我想将一些变量(字典、查询集等)传递给模板。我想从 all_q
字典中访问 individual 值,但是我尝试的命令 none 像:all_q.author
或 all_q.0.1
工作。是我的观点错了还是我模板中的代码错了? (第二个 div 中的循环工作正常)
查看:
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})
模板:
<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>
您可以像这样访问 "indiviual values":
{{questions.0.author}}
{{questions.1.author}}
....
{{questions.42.author}}
等Question
uninitilaized 这样不会做你想做的事。也许 Question.objects.all()?
。确实不清楚您要做什么。
我是 Django 的新手,正在努力完成我认为是一项简单的任务。我想将一些变量(字典、查询集等)传递给模板。我想从 all_q
字典中访问 individual 值,但是我尝试的命令 none 像:all_q.author
或 all_q.0.1
工作。是我的观点错了还是我模板中的代码错了? (第二个 div 中的循环工作正常)
查看:
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})
模板:
<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>
您可以像这样访问 "indiviual values":
{{questions.0.author}}
{{questions.1.author}}
....
{{questions.42.author}}
等Question
uninitilaized 这样不会做你想做的事。也许 Question.objects.all()?
。确实不清楚您要做什么。