Continued from Request Processing
Currently, urls.py is called when / posts / is accessed, and the process of directly returning and displaying "Hello, World!" From views.py is described. Now let's create a template and have it displayed.
From now on, we will create a template, rewrite views.py, and change the process to routing → views → template.
First, create a "templates" folder in the application folder (this time the "posts" folder), and then create a folder with the same name as the application folder in it.
Next, create an HTML file in the posts folder inside templates.
index.html
<!DOCTYPE html>
<html lang="ja-jp" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h2>This is a test page.</h2>
</body>
</html>
In addition, modify views.py to display the HTML file you created.
views.py
def index(request):
#return HttpResponse("Hello, World!")← Comment out here
return render(request, 'posts/index.html') #Newly added line
By doing this, when views.py is called, "Hello, World!" Is not returned directly, but it can be returned by referring to index.html in the posts folder in templates. If you start the Django server and display it as "http://127.0.0.1:8000/posts/", it will be as follows.
Recommended Posts