[PYTHON] WEB application development using Django [Template addition]

Continued from Request Processing

template

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.

Add template

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. テンプレート.png

Recommended Posts

WEB application development using Django [Template addition]
WEB application development using Django [Application addition]
WEB application development using Django [Django startup]
WEB application development using Django [Model definition]
WEB application development using Django [Initial settings]
WEB application development using Django [Request processing]
WEB application development using django-Development 1-
WEB application development using Django [Admin screen creation]
Application development using SQLite with Django (PTVS)
Development of WEB application using Django [Add data from management screen]
Web application using Bottle (1)
Web application development with Flask
Web application creation with Django
Build a web application with Django
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Application development using Azure Machine Learning
Django # 2 (template)
Try using the web application framework Flask
Web application development in Go language_Hands-on day 1
About Cloud run + Firebase development [Web application development]
I made a WEB application with Django
How to build an application from the cloud using the Django web framework
Django HTML Template # 2
Until Django application creation by terminal (development environment)
Django Template notes
Django HTML template
Django Template Tips
First Django development
Web App Development Practice: Create a Shift Creation Page with Django! (Write a base template)
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
Build a Django development environment using pyenv-virtualenv on Mac
Web application made with Python3.4 + Django (Part.1 Environment construction)
Tech-Circle Let's start application development using machine learning (self-study)
Prepare Django development environment using homebrew on MacOSX Mavericks (10.9)
Django template file organization
Development digest with Django
Django2 screen addition flow
Try using Django templates.html
[Memo] Django development environment
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2