Style the new_topic Page

  • To ensure that all pages look consistent, we must load django_bootstrap5 and add to the page_header block an appropriate message.

new_topic.html

html
1{% extends "code_journals/base.html" %}
2{% load django_bootstrap5 %}
3
4{% block page_header %}
5  <h2>Add a New Topic:</h2>
6{% endblock page_header %}
7
8{% block content %}
9  <form action="{% url 'code_journals:new_topic' %}" method='post' class="form">
10  {% csrf_token %}
11  {% bootstrap_form form %}
12  {% bootstrap_button "Add Topic" button_type="submit" button_class="btn-primary" %}
13  </form>
14{% endblock content %}
  • Once the page_header block has been added (in line 4), we add the class=ā€œformā€ attribute to the
    tag in line 9.
  • On line 11, the {% bootstrap_form form %} template tag is used instead of {{ form.as_p }}. Similarly, in line 12, the bootstrap button template tag is used to style the button element.

Style the topics Page

  • Next, let’s make sure the pages for viewing the information are styled as well:

topics.html

html
1{% extends "code_journals/base.html" %}
2
3{% block page_header %}
4    <h1>Topics</h1>
5{% endblock page_header %}
6
7{% block content %}
8
9<ul>
10    {% for topic in topics %}
11        <li>
12            <h3>
13              <a href="{% url 'code_journals:topic' topic.id %}">{{ topic }}</a>-
14              <a href="{% url 'code_journals:delete' topic.id %}">Delete</a>
15            </h3>
16        </li>
17    {% empty %}
18        <li>No topics have been added yet.</li>
19    {% endfor %}
20</ul>
21
22<h3><a href="{% url 'code_journals:new_topic' %}">Add a New Topic:</a></h3>
23
24{% endblock content %}
  • Note that we don’t need the {% load django_bootstrap5 %} tag, because we’re not using any custom bootstrap template tags in this file.
  • In line 4, add the heading Topics inside the page_header block.
  • Style each topic as an < h3> element to make them larger on the page in lines 12 through 15, and do the same for the hyperlink to add a new topic on line 22.

Style the Entries on the Topic Page

  • We’ll use Bootstrap’s cards to make each entry stand out.
  • A card can be placed inside a < div> tag with predefined styling and is appropriate for displaying a topic’s entry:

topic.html

html
1{% extends 'code_journals/base.html' %}
2
3{% block page_header %}
4    <h2>{{ topic }}</h2>
5{% endblock page_header %}
6
7{% block content %}
8
9    <p>
10        <a href="{% url 'code_journals:new_entry' topic.id %}">add new entry</a>
11    </p>
12    
13{% for entry in entries %}
14    <div class="card mb-3"><!-- Open card -->
15        <h4 class="card-header">
16                {{ entry.date_added|date:'M d, Y H:i' }}
17                <small><a href="{% url 'code_journals:edit_entry' entry.id %}">edit entry</a></small>
18        </h4>
19        <div class="card-body">
20            {{ entry.text|linebreaks }}
21        </div>
22    </div><!-- Close card -->
23    
24{% empty %}
25        <p>There are no entries for this topic yet.</p>
26{% endfor %}
27
28
29{% endblock content %}
  • On line 3, we first place the topic in the page_header block. Then, delete the unordered list structure previously used in this template.
  • Instead of making each entry a list item, we create a card div element in line 14, which contains two more nested divs: a card-header div on line 15 and a card-body div on line 19.
    • Note the card-header div contains the date for the entry and the link to edit the entry. Both are styled as < h4> elements, however add the < small> tags around the edit_entry as shown on line 17.
  • On line 23, is the card-body div, which contains the actual text of the entry. Notice that the Django code for including the information on the page hasn’t changed at all, only the elements that affect the appearance of the page have changed.