[PYTHON] What is a dog? Challenge Django templates! Volume

Template = HTML with embedded variables and processing

This is Ponta, a Shiba Inu. The other day I was chatting with my online friends pretending to be human, but I started to think that the other person might be a dog pretending to be human. I wonder if I should come out.

Well, today I'm going to challenge Django templates. A template is a web page HTML with variables and processes embedded in it, and creates a screen to insert and display values here.

Directory structure for templates

Create folders templates, templates / wan under the directory wan, and a template file index.html under templates / wan. The tree looks like this.

(venv_dog) Ponta@shiba_app # tree
.
// ...abridgement
└── wan
  // ...abridgement
    ├── templates
    │   └── wan
    │       └── index.html
   // ...abridgement

Template description (index.html)

The template index.html was written as follows.

wan/templates/wan/index.html


<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>{{ title }}</title>
</head>
<body>
	<h1>{{ title }}</h1>
	<p>{{ year }}Year{{ month }}Month{{ date }}Day</p>
	<p>{{ description }}</p>
</body>
</html>

Put the variable inside {{}}. Here the variables are title, year, month, date, description.

Template call (views.py)

The template is called from views.py, so rewrite views.py as follows.

wan/views.py


from django.shortcuts import render

def index(request, dogname, diarydate):
    year = diarydate[:4]
    month = diarydate[4:6]
    date = diarydate[6:]
    
    title = dogname + "s diary"
    description = "Today's rice is yakiniku!"

    params = {
        'year': year,
        'month': month,
        'date': date,
        'description': description,
        'title': title,
    }
    
    return render(request, 'wan/index.html', params)

Originally, the procedure would be to call the template with django.template.loader.get_template ('wan / index.html'), set the template variables with the render function, and then return the HttpResponse object as before. But thankfully Django has a render () function for the shortcut, so I'm using it.

The render () function takes three arguments, a request object, a template name, and a parameter, renders it, and returns an HttpResponse object. [^ 1]

Django app registration (settings.py)

In Django, when you use a template, you need to register the application to be used in the project. Therefore, register the app wan in the variable INSTALLED_APP described in shiba_app / settings.py.

shiba_app/settings.py


// ...abridgement
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'wan',
]
// ...abridgement

The last line ('wan',) is added.

Display test

Let's display it. スクリーンショット 2020-08-26 7.01.00.png

Yesterday's rice was yakiniku. I'm a dog, so I don't need sauce. See you dear! one!

[^ 1]: Create your first Django app, part 3 Write a view that actually works

Recommended Posts

What is a dog? Challenge Django templates! Volume
What is a dog? Django installation volume
What is a dog? Python installation volume
What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Django App Creation Start Volume--startapp
What is a dog? Django App Creation Start Volume--startproject
What is Django? .. ..
What is a dog? Volume of GET request and query parameters
What is a dog? Django--Get Name and Date from URL Volume
What is a dog? Django--Create a custom user model
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
What is a dog? Django--Create a custom user model 2
To myself as a Django beginner (2) --What is MTV?
What is a decision tree?
What is a Context Switch?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a callback function?
What is a python map?
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
[Python] What is a zip function?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
It's a Mac. What is the Linux command Linux?
Tell me what a conformal map is, Python!
What is namespace
What is copy.copy ()
What is dotenv?
What is POSIX?
First Django Challenge
What is a dog? Django--Volume of using values obtained from URLs in class-based views
What is Linux
What is klass?
What is SALOME?
What is Linux?
What is python
What is hyperopt?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
What I did when I stumbled on a Django tutorial
Basics of Python learning ~ What is a string literal? ~
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python