HTML From a View
Part of the Django Basics section of Coddy's Django journey. Lesson 3 of 23.
The home page is one line of plain text. A real page has headings and paragraphs, written in HTML, the language of web pages: <h1> marks a heading, <p> a paragraph.
A view can return HTML in its string, and the browser draws it instead of showing the tags:
def news(request):
return HttpResponse("<h1>News</h1><p>New cakes on Friday.</p>")Every tag you open needs its closing tag, with a slash: </h1>. Forget it and the rest of the page becomes part of the heading.
A whole page in a Python string gets messy. Templates fix that later in this section; for two lines, a string is fine.
Challenge
EasyMake the home page a heading and a paragraph:
- the heading
<h1>Welcome to Corner Café</h1>, - then the paragraph
<p>Fresh coffee and cake every day.</p>.
Press Run: the home page shows a big heading over a line of text.
Try it yourself
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to Corner Café")
def about(request):
return HttpResponse("Corner Café opened in 2026.")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Django Basics
Practice on your own: Django playground