Django 'context' 用于基于函数和基于 class 的视图
Django 'context' for function-based and class-based views
帮助我了解如何使用 Django 的 context
--问题是否与在基于函数的视图和基于 class 的视图中使用 context
有关:
我创建了两个视图(index
和 post_list
)和各自的模板(在 Mozilla Django Tutorial 的评估部分之后)。 index
的代码有效,但 post_list
的相同代码无效。这是为什么?
查看#1
def index(request):
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_comments = Comment.objects.all().count()
num_authors = Author.objects.count()
num_commenters = Commenter.objects.count()
context = {
'num_posts': num_posts,
'num_comments': num_comments,
'num_authors': num_authors,
'num_commenters': num_commenters,
'post_list' : post_list,
}
return render(request, 'index.html', context=context)
模板 #1 --有效:
{% block content %}
<h1>Index:</h1>
This blog has a total of {{num_posts}} posts by {{ num_authors}} authors.
{% endblock %}
查看#2
class PostListView(generic.ListView):
model = Post
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_authors = Author.objects.count()
template_name = 'blog/post_list.html'
context = {
'num_posts': num_posts,
#'num_authors': num_authors, # Removed b/c it doesn't work
'post_list' : post_list,
}
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context['num_authors'] = Author.objects.count() # But this works!
return context #edited, this line was left out in the original post
模板#2 - 不完全有效:
{% block content %}
<h1>All Posts:</h1>
This blog has a total of {{num_posts}} posts by {{ num_authors}} authors.
{% endblock %}
您对 get_context_data
方法的定义不会更新您希望在模板中使用的所有变量。例如,context
class 变量与您在 get_context_data
方法中返回的 context
变量不是一回事。因此,您在模板中可以访问的唯一变量是 num_authors
。为确保您的模板中包含所有需要的变量,您需要编辑 get_context_data
以使用在 class 级别定义的字典更新 context
:
class PostListView(generic.ListView):
model = Post
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_authors = Author.objects.count()
template_name = 'blog/post_list.html'
context_vars = {
'num_posts': num_posts,
'num_authors': num_authors,
'post_list' : post_list,
}
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context.update(PostListView.context_vars)
return context
对您的原始代码片段进行了两项主要更新:class 变量 context
更改为 context_vars
以避免冲突和混淆; get_context_data
方法中的 context
变量是 updated
,内容为 context_vars
。这将确保在 class 级别(即 PostListView.context_vars
)定义的所有内容都进入您的模板。
帮助我了解如何使用 Django 的 context
--问题是否与在基于函数的视图和基于 class 的视图中使用 context
有关:
我创建了两个视图(index
和 post_list
)和各自的模板(在 Mozilla Django Tutorial 的评估部分之后)。 index
的代码有效,但 post_list
的相同代码无效。这是为什么?
查看#1
def index(request):
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_comments = Comment.objects.all().count()
num_authors = Author.objects.count()
num_commenters = Commenter.objects.count()
context = {
'num_posts': num_posts,
'num_comments': num_comments,
'num_authors': num_authors,
'num_commenters': num_commenters,
'post_list' : post_list,
}
return render(request, 'index.html', context=context)
模板 #1 --有效:
{% block content %}
<h1>Index:</h1>
This blog has a total of {{num_posts}} posts by {{ num_authors}} authors.
{% endblock %}
查看#2
class PostListView(generic.ListView):
model = Post
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_authors = Author.objects.count()
template_name = 'blog/post_list.html'
context = {
'num_posts': num_posts,
#'num_authors': num_authors, # Removed b/c it doesn't work
'post_list' : post_list,
}
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context['num_authors'] = Author.objects.count() # But this works!
return context #edited, this line was left out in the original post
模板#2 - 不完全有效:
{% block content %}
<h1>All Posts:</h1>
This blog has a total of {{num_posts}} posts by {{ num_authors}} authors.
{% endblock %}
您对 get_context_data
方法的定义不会更新您希望在模板中使用的所有变量。例如,context
class 变量与您在 get_context_data
方法中返回的 context
变量不是一回事。因此,您在模板中可以访问的唯一变量是 num_authors
。为确保您的模板中包含所有需要的变量,您需要编辑 get_context_data
以使用在 class 级别定义的字典更新 context
:
class PostListView(generic.ListView):
model = Post
post_list = Post.objects.all()
num_posts = Post.objects.all().count()
num_authors = Author.objects.count()
template_name = 'blog/post_list.html'
context_vars = {
'num_posts': num_posts,
'num_authors': num_authors,
'post_list' : post_list,
}
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context.update(PostListView.context_vars)
return context
对您的原始代码片段进行了两项主要更新:class 变量 context
更改为 context_vars
以避免冲突和混淆; get_context_data
方法中的 context
变量是 updated
,内容为 context_vars
。这将确保在 class 级别(即 PostListView.context_vars
)定义的所有内容都进入您的模板。