Skip to main content
 首页 » 编程设计

python之在 TemplateView 中使用变量

2024年11月24日6luoye11

我曾经在 Django 中用这样的函数定义我的页面:

 def pdf(request): 
     return render(request, 'blog/pdf.html', {'title': 'PDF files'}) 

我在哪里使用 Title var 作为 html 页面标题。我开始为我的页面使用 TemplateView 类,但我不确定如何在类似这样的内容中使用相同的 title:

class About(LoginRequiredMixin, TemplateView): 
    template_name = 'blog/about.html' 

请您参考如下方法:

试试这个,

class About(LoginRequiredMixin, TemplateView): 
    template_name = 'blog/about.html' 
 
    def get_context_data(self, *args, **kwargs): 
        context = super(About, self).get_context_data(*args, **kwargs) 
        context['title'] = 'PDF files' 
        return context