[PYTHON] Django # 2 (template)

Use templates in Django

Setting for the time being. Edit the following file.

--App /views.py --Call the template file from the function (view function) defined in views.py and return the HTTP response.

Write a view function

Contents corresponding to hello for the time being.

python/app_name/views.py


from django.shortcuts import render  # render(request, 'hello.html', context)
from django.http import HttpResponse  # HttpResponse('Hello, World !!')
from django.shortcuts import render
from django.views import View

class HelloView(View):
    def get(self, request, *args, **kwargs):
        context = {
            'message': "Hello World! from View!!",
        }
        return render(request, 'hello.html', context)

hello = HelloView.as_view()

A function in which the render function calls a template to generate HTML.

--Write the request of the first argument for the time being. --Specify the template file with the second argument'hello.html'. Specify a relative path under templates / folder. --The third argument context is a dictionary of variables to pass to the template. --You can refer to the value by using the key as the variable name in the template file. --Since flexible variables cannot be added or changed with the template file name, all of them are packed in the basic context.

The last hello = HelloView.as_view () is an operation that associates the HelloView class as a hello view function. This function name corresponds to the function name set in urls.py.

Set the location of the template

Set the base location for the template file. The following is set in project folder / templates /.

config/settings.py


57c58
<         'DIRS': [],
---
>         'DIRS': [os.path.join(BASE_DIR, 'templates')],

Prepare a template file

Create the directory described in config / settings.py above Create a template file in it.

$ mkdir templates
$ vi templates/hello.html

The contents of the template file. This time just display the variable message. The detailed writing method is as follows. The Django Template Language |Django documentation| Django

templates/hello.html


{{ message }}

Operation check

With a web browser at http: // IP address: 8000 / app_name / OK if Hello World! From View !! is displayed

Recommended Posts

Django # 2 (template)
Django HTML Template # 2
Django Template notes
Django HTML template
Django Template Tips
Django
template
Template registration from Django Bootstrap
Show Django ManyToManyField in Template
Django note 4
Django memorandum
Django installation
Django test
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Make a filter with a django template
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
How to do arithmetic with Django template
(Note) Template file search order in Django
WEB application development using Django [Template addition]
(For myself) Django_1 (Basic / Hello World / Template)
Resolve Angural JS and Django template conflicts
Write a short if-else for Django Template
Django environment construction
Django ~ settings.py edition ~
Django Contact Form 2
Django begins part 1
Django model: ManyToManyField
What is Django? .. ..
Models in Django
Python Django Tutorial (5)
Django Learning Memo
Python Django Tutorial (2)
[Django] as_view () notes
First Django Challenge
Django related sites
Internationalization with django
Django version check
django table creation
Django begins part 4
Django 1.9 for internationalization
CentOS8 --Play --Django
CentOS8 --Install --Django
django tutorial memo
[Django] Redo migrate