Templates
In Exercise 3.01, Implementing a Simple Function-Based View, we saw how to create a view, do the URL mapping, and display a message in the browser. But if you recall, we hardcoded the HTML message Welcome to Bookr!
in the view function itself and returned an HttpResponse
object, as follows:
message = f"<html><h1>Welcome to Bookr!</h1> "\ "<p>{Book.objects.count()} books and counting!</p></html>" return HttpResponse(message)
Hardcoding of HTML inside Python modules is not a good practice, because as the content to be rendered in a web page increases, so does the amount of HTML code we need to write for it. Having a lot of HTML code among Python code can make the code hard to read and maintain in the long run.
For this reason, Django templates provide us with a better way to write and manage HTML templates. Django's templates not only work with static HTML content but also dynamic HTML templates.
Django...