0Pricing
Django Academy · 课时

重写 get_queryset 与模板

为每个视图自定义数据和模板名称

重写 get_queryset 与模板 是 CoddyKit 上的免费 Django Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Make Generic Views Yours

Generic views give sensible defaults, but real apps need tweaks. You override a few hooks to customize them. 🔧

What get_queryset Does

get_queryset decides which rows a view works with, so overriding it lets you filter or sort the results.

Filtering with get_queryset

Return a filtered queryset to show only the rows you want, like published posts.

class PostList(ListView):
    model = Post
    def get_queryset(self):
        return Post.objects.filter(published=True)

Reading the URL or User

Inside get_queryset you can read self.request, so you can scope results to the logged-in user.

def get_queryset(self):
    return Post.objects.filter(author=self.request.user)

Override template_name

Do not like the default template path? Set template_name to point at any template you choose.

class PostList(ListView):
    model = Post
    template_name = "blog/home.html"

Override the Context Variable

Pair template_name with context_object_name so your custom template reads with a friendly variable.

class PostList(ListView):
    template_name = "blog/home.html"
    context_object_name = "posts"

Add Extra Context

Override get_context_data to pass extra values, like a page title, alongside the objects.

def get_context_data(self, **kwargs):
    ctx = super().get_context_data(**kwargs)
    ctx["title"] = "Blog"
    return ctx

Always Call super First

In get_context_data, call super() first so you keep the objects the view already added, then add your own.

Customize the Suffix

Prefer model_browse over model_list? Set template_name_suffix to change just the suffix Django appends.

class PostList(ListView):
    template_name_suffix = "_browse"

Order the Results

Override get_queryset to sort rows, for example showing the newest posts first.

def get_queryset(self):
    return Post.objects.order_by("-created")

Small Hooks, Big Control

With these few overrides you bend generic views to almost any need while keeping their tidy structure. 💡

Quick Check

What is the purpose of overriding get_queryset?

Recap: Customizing Views

You shaped generic views with get_queryset, template_name, and get_context_data, customizing data and templates without losing their simplicity. 🎉

常见问题解答

「重写 get_queryset 与模板」课时是免费的吗?

是的 — 「重写 get_queryset 与模板」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「重写 get_queryset 与模板」这节课中我会学到什么?

为每个视图自定义数据和模板名称 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「重写 get_queryset 与模板」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. ListView 与 DetailView
  2. CreateView 与 UpdateView
  3. DeleteView 与 success_url
  4. 重写 get_queryset 与模板
← 返回 Django Academy