In this lesson, you’re going to add some Bootstrap style to what you’ve created so far in your template to make it look a bit nicer.
Bootstrap Template Issues
If you copy the Bootstrap code from the associated tutorial without applying any changes, then you will run into an error. As eawongtheprogrammer mentions in their comment, this is because of the following line:
<a href="{% url 'projects:project_detail' project.pk %}" class="btn btn-primary">
Read More
</a>
The code is referencing a route that you haven’t built yet, which throws Django into light confusion. Change the href
attribute value to a hashtag (#
) to fix this issue:
<a href="#" class="btn btn-primary">
Read More
</a>
You’ll eventually add this link back once the supporting back-end is set up.
Minimal Working Template
As posted by Paul, you can use the minimal template that he provided instead:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Projects</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<h1>Projects</h1>
<div class="row">
{% for project in projects %}
<div class="col-md-4">
<div class="card mb-2">
<img class="card-img-top" src="{% static project.image %}">
<div class="card-body">
<h5 class="card-title">{{ project.title }}</h5>
<p class="card-text">{{ project.description }}</p>
<a href="#"
class="btn btn-primary">
Read More
</a>
</div>
</div>
</div>
{% endfor %}
</div>
</body>
</html>
If you run into an error, keep training your debugging muscles 💪 and carefully read the error messages that Django provides. Make sure to also always check the Comments & Discussion section of a lesson, to find out whether someone else ran into the same issue and found a solution for it.
Gascowin on Oct. 20, 2019
Hi, you mentioned that the html you copy-pasted is in “the associated article”. Can you please direct me to this? I cannot for the life of me seem to find it anywhere. Thanks.