【发布时间】:2017-09-23 15:22:45
【问题描述】:
我正在尝试了解 django 的工作原理,但我对视图有疑问。
下面的代码
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
我理解字典,但我不知道为什么{'posts': posts} 是同一个单词的字典,并且值没有引号。
当我使用字典时,我会使用以下内容:
hello = {'my_key': 'this is text', 'my_key2': 2017 }
{'posts': posts},在这个例子中帖子显示了两次,第二次,我的意思是这个值没有引号。
谁能解释一下?
【问题讨论】:
-
不知道为什么你认为这些有什么不同。引号中的第一个元素是键;第二个是价值。
-
这和你已经知道的字典一模一样,
"posts"是键,posts是值,过滤后的对象。键将是在模板中调用您的值的一种方式,也可以是render(request, 'blog/post_list.html', {'filtered_posts': posts})
标签: python django dictionary django-views