有什么好的方法可以在Django中获取页面的动态标题?

问题描述 投票:1回答:1

我想在Twitter上定制一个分享按钮,我用这个来分享。

http:/twitter.comintenttweet?status=[TITLE]+[URL]。

我可以用{{ request.get_host }}和{{ request.path }}来获取URL,但似乎无法获取标题。有没有什么请求对象可以用来获取当前页面的标题?谢谢。

python html django twitter social-networking
1个回答
1
投票

最简单的方法是:在你的base.html中插入以下代码。

#base.html
{% block title %} {% endblock %}

并在您的索引或任何Html文件中使用您的标题。

#index.html
{% extends 'base.html'%}
{% block title %} My Page Title {% endblock %}

0
投票

将你的上下文对象键放在双解析中.以我在学校管理系统中做一个动态的教师页面为例。

#urls.py
path("teacher/<str:pk>/",views.teacher,name="teacher"),

The vew

#view
def teacher(request,pk):
    teacher = Teacher.objects.get(id=pk)
    context= {"teacher":teacher}
    return render(request,'main/teacher.html',context)

模板

 <title>{{teacher.fname}} Page</title>
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.