Updated the root urls.py to set a path for test_view
views.py
from django.http import HttpResponse
def test_view(request):
return HttpResponse("""<html><body>
<h1>Hello World!</h1>
<p>This is my first <strong>html</strong> web app!</p>
</body></html>""")
Root urls.py
from django.contrib import admin
from django.urls import path
from blog.views import test_view
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', test_view),
]
Let’s Take Advantage of Templates
Use the render function:
Arguments we will use in order:
The HTTP request
The template file name
The context (optional)
Updated test_view Function
from django.shortcuts import render
def test_view(request):
return render(request, 'test.html')
The Result
Django cant find text.html
The Solution
Inside the poll directory, create a new directory named templates
Inside the temapltes directory, create a new file named test.html
Edit test.html to look like this:
<html>
<body>
<h1>Hello World!</h1>
<p>This is my first <strong>html</strong> web app!</p>
</body>
</html>
Template Based Application
The application works and looks the same
It now separates HTML from Python code
HTML placed in templates
Python code in .py files
To change how the application looks the designers edit test.html
HTML Tags
Every HTML <tag> will have a matching closing </tag>
For example:
<h1>heading 1</h1>
Tags can contain other tags:
<div>
<p>
This is a paragraph inside a div
</p>
</div>
Some tags require specific tags inside them
Like LI inside UL or OL tags:
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
Useful HTML Tags
A for creating links (known as anchor)
H1 to H4 for headings
DIV for page sections, used to create structure
Example: Menu, header, footer, content ..etc
P for paragraphs, used to divide textual content
OL/UL for ordered/unordered lists and TABLE
BR for line breaks, no closing tag, written as:
<BR />
Links (Anchors)
Syntax:
<a href="http://google.com">My Text</a>
Will turn “My Text” into a link, if clicked will open google’s homepage