We can start to build out the Learning Log project. We’ll create two pages that display data: a page that lists all topics and a page that shows all the entries for a particular topic.
We’ll create a base template that all templates can inherit from it. For each of these pages we’ll create url,view and template .
Template Inheritance
There are some elements which come in every page. So,we have to write every time .To overcome this repetition in every page we’ll use base.html when child page need elements it can Inheritance from base. html .
The Parent Template
We’ll start with base.html template. This parent template will store the element which is common to all page. The child template Inheritance from this parent block. Here,the common element is title at every page,which will display on all child page .
base.html <p> ➊ <a href=”{%url ‘learning_logs:index’%}”>Learning Log</a>
➋ {% block content %}{% endblock content %}
At ➊ In this example, the template tag {% url ‘learning_logs:index’ %} generates a URL matching the URL pattern defined in learning_logs/urls.py with the name ‘index’.
At ➋ we insert a pair of block tags. This block, named content, is a placeholder; the child template will define the kind of information that goes in the content block.
A child template doesn’t have to define every block from its parent, so you can reserve space in parent templates for as many blocks as you like, and the child template uses only as many as it requires.
NOTE:-
In Python code, we almost always indent four spaces. Template files tend to have more levels of nesting than Python files, so it’s common to use only two spaces for each indentation level.
The Child Template
Now we need to rewrite index.html to inherit from base.html. Here’s index.html:
index.html
➊ {% extends “learning_logs/base.html” %}
➋ {% block content %}
<p>Learning Log helps you keep track of your learning, for any topic you’re learning about.</p>
➌ {% endblock content %}
we’ve replaced the Learning Log title with the code for inheriting from a parent template ➊. A child template must have an {% extends %} tag on the first line to tell Django which parent template to inherit from. This line pulls everything from base.html and child template to reserve in content block.
We define the content block at ➋ by inserting a {% block %} tag with the name content. Everything that we aren’t inheriting from the parent template goes inside a content block. Here, that’s the paragraph describing the Learning Log project.
The Topics Page
We have to display two pages : the general topics page and the page displays entries for single topic page. The topics page will show all topics that users have created, and it’s the first page that will involve working with data.
The Topics URL Pattern
First, we define the URL for the topics page . It’s common to choose a simple URL fragment that reflects the kind of information presented on the page. We’ll use the word topics, so the URL http://localhost:8000/topics/ will return this page. Here’s how we modify learning_logs/urls.py:
urls.py
“””Defines URL patterns for learning_logs.”””
–snip–
urlpatterns = [
# Home page
path(”, views.index, name=’index’),
# show all topics
➊ path(‘topics/’, views.topics, name=’topics’),
]
We’ve simply added topics/ into the regular expression argument used for the home page URL ➊. When Django examines a requested URL, this pattern will match any URL that has the base URL followed by topics.
There can’t be anything else after the word topics, or the pattern won’t match. Any request with a URL that matches this pattern will then be passed to the function topics() in views.py .
The Topics View
The topics() function needs to get some data from the database and send it to the template. Here’s what we need to add to views.py:
views.py
from django.shortcuts import render
➊ from .models import Topic
def index(request):
–snip–
➋ def topics(request):
“””Show all topics.”””
➌ topics =Topic.objects.order_by(‘date_added’)
➍ context = {‘topics’: topics}
➎ return render(request, ‘learning_logs/topics.html’, context)
We first import the model associated with the data we need ➊.The topics() function needs one parameter: the request object Django received from the server ➋.At ➌ we query the database by asking for the Topic objects, sorted by the date_added attribute. We store the resulting queryset in topics.
At ➍ we define a context that we’ll send to the template. A context is a dictionary in which the keys are names we’ll use in the template to access the data and the values are the data we need to send to the template. In this case, there’s one key-value pair, which contains the set of topics we’ll display on the page.
When building a page that uses data, we pass the context variable to render() as well as the request object and the path to the template ➎.
































