Наследование шаблонов не работает - Учебник по Django
Я следую учебнику по Django от Кори Шафера . Я дошел до того момента, когда мне нужно создать наследование шаблона base.html. После настройки всего в соответствии с моим проектом и запуска сервера, моя веб-страница представляется в виде исходного кода в формате html. нажмите, чтобы увидеть страницу после запуска сервера.
Мой код views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
# Create your views here.
def index(request):
text = "welcome to the website - python"
posts = [
{
'title': 'The Fault in Our Stars', 'price': '3.0 BD', 'post_date': '24-10-2021'
},
{
'title': 'The Black Swan', 'price': '3.5 BD', 'post_date': '23-10-2021'
},
{
'title': 'Watchmen', 'price': '5.0 BD', 'post_date': '22-10-2021'
}
]
context = {
'text': text, 'posts': posts
}
return render(request, 'blog/index.html', context, {'title': 'Bookish Bahrain - Home'})
Мой код base.html:
<!DOCTYPE html>
<html lang="en">
<head>
{% if title %}
<title> {{ title }} </title>
{% else %}
<title> Bookish Bahrain </title>
{% endif %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Мой код index.html:
{% extends "blog/base.html" %}
{% block content %}
{% for post in posts %}
<p> {{post.title}} </p>
<p> {{post.price}} </p>
<p> {{post.post_date}} </p>
{% endfor %}
{% endblock content %}
Моя версия Django - 3.9.7.
Ваша помощь будет высоко оценена.
Вы просто должны сделать это:
context = {
'text': text, 'posts': posts,
'title': 'Bookish Bahrain - Home'
}
return render(request,'blog/index.html', context )
Посмотрите здесь: https://docs.djangoproject.com/en/3.2/topics/http/shortcuts/