Maximus Vermillion
Maximus Vermillion

Reputation: 709

What is a good way to get the dynamic title of a page in Django?

I'm trying to customize a share on Twitter button and I'm using to this to share:

http://twitter.com/intent/tweet?status=[TITLE]+[URL]

I'm able to get the URL with {{ request.get_host }} and {{ request.path }}, but I can't seem to get the title working. Are there any request objects I can use to get the title of my current page title? Thanks.

Upvotes: 3

Views: 6306

Answers (2)

Shirish Chaudhari
Shirish Chaudhari

Reputation: 271

The easiest way to do this is: Insert the following code in your base.html

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

and following in your index or any Html file with your title

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

Upvotes: 9

Robinson Ngecu
Robinson Ngecu

Reputation: 168

Place your context object key in double paranthesis. Lets take am making a dynamic teacher's page in a school management system

#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)

The template

 <title>{{teacher.fname}} Page</title>

Upvotes: 0

Related Questions