[PYTHON] Create a shogi game record management app using Django 4 ~ Create View ~

Introduction

This is ** 4th **, a memorandum of making a game record management app for shogi using Django.

Work environment

The working environment this time is as follows

Also, Django's directory structure looks like this:

- kifu_app_project/
    - kifu_app_project/
        - __init__.py
        - setting.py
        - urls.py
        - wsgi.py
    - kifu_app/
        - migrations/
        - __init__.py
        - admin.py
        - apps.py
        - models.py
        - tests.py
        - views.py
    - manage.py
    - .gitignore

Contents of this article

--URL setting --View settings --Create Template

URL settings

First, set the URL. Decide that "when you come to this URL, do this method!" The function that determines what kind of page is displayed for a specified URL in this way is called "URL dispatcher".

Furthermore, the behavior of this URL dispatcher is decided to be written in a configuration file called "URLConf". In Django, urls.py is the URLConf.

The following is reference material.

[[Introduction to Django] Roles and usage of urls.py (URLConf)] 1

Rough URL settings

First, let's load the URL settings in the kifu_app application for the entire Django project Edit urls.py in the inner kifu_app_project as follows.

kifu_app_project/urls.py


"""kifu_app_project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include       #add include

urlpatterns = [
    path('kifu_app/', include('kifu_app.urls')),      #add to
    path('admin/', admin.site.urls),
]

The URL settings in the kifu_app are now loaded!

Detailed URL settings

Next, specify the detailed URL in the kifu_app application.

Create urls.py in kifu_app.

What you do with this urls.py is

  1. Import all methods in views.py
  2. Specify the application name
  3. Write the URL is.

kifu_app/urls.py


from django.urls import path

from . import views     # 1.

app_name = 'kifu_app'      # 2.

urlpatterns = [
    path('', views.index, name='index'),    # 3.
]

Describe the URL in the first argument of the path function and the method to be called in the second argument. The third argument (name attribute) can be pear, but if you specify it, it will be easier later.

Earlier, in urls.py in kifu_app_project, the URL of the kifu_app application was set to 'kifu_app /'. So, for example, note that the URL where index.html is displayed is * localhost: 8000 / kifu_app / *.

View settings

Next, create a method to be executed on the server side when displaying the Template (screen). This allows you to view the data dynamically.

Edit views.py

Since I wrote earlier that the index method is called in urls.py, I will actually create the index method. Here, let's try to get today's date and pass it to Template.

views.py


from django.shortcuts import render

# Create your views here.

import datetime

def index(request):
    today = datetime.date.today()
    return render(request, 'index.html', {'today': today})

View is displayed by returning the render method. The argument is --Variable request, --template file name --Dictionary type variable you want to pass I will pass it at.

Creating Template

Finally, we will create the actual screen (Template).

Creating index.html

In View, I wrote that I will pass data to index.html, so I will create index.html. The writing method is almost the same as when writing HTML.

index.html


<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>Kifu APP</title>
    </head>
    <body>
        <h3>{{ today }}</h3>
    </body>
</html>

If you want to use the variable passed from View, write the dictionary key in {{}}.

Now start the server and let's access it! It's OK if today's date comes out!

Next time preview

[Pass DB data to Template] 2

Recommended Posts

Create a shogi game record management app using Django 4 ~ Create View ~
Create a shogi game record management app using Django 2-Database settings-
Create a shogi game record management app using Django 6 ~ Template division ~
Create a shogi game record management app using Django 3 ~ Django default management site settings ~
Creating a Shogi Game Record Management App Using Django 1-Environment Construction-
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
Create a simple CRUD app using Django's generic class view
Create and deploy a Django (PTVS) app using Azure Table storage
Create a beauty pageant support app using PyLearn2
Create a Mac app using py2app and Python3! !!
Until you create a new app in Django
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
Create a Django schedule
Create a Todo app with Django REST Framework + Angular
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Create a Todo app with the Django REST framework
DJango Note: From the beginning (using a generic view)
Create a Todo app with Django ③ Create a task list page
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
Create a Todo app with Django ⑤ Create a task editing function
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create a game to control puzzle & dragons drops using pygame
Create a homepage with django
[Learning record] Create a mysterious dungeon game with Pyhton's Tkinter
Create a Django login screen
Create a Todo app with Django ① Build an environment with Docker
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Create a python GUI using tkinter
Steps to create a Django project
Create a nested dictionary using defaultdict
Implement a Django app on Hy
Create a CRUD API using FastAPI
Create a C wrapper using Boost.Python
Create a file uploader with Django
Create a LINE Bot in Django
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
Create a record with attachments in KINTONE using the Python requests module
Create a Todo app with Django ④ Implement folder and task creation functions
Create a model for your Django schedule
I tried playing a ○ ✕ game using TensorFlow
Create a simple GUI app in Python
Create a graph using the Sympy module
Create a GUI app with Python's Tkinter
[Python] Create a Batch environment using AWS-CDK
Create a simple web app with flask
Create a Python-GUI app in Docker (PySimpleGUI)
Ajax in Django (using generic class view)
Get a reference model using Django Serializer
Create your first app with Django startproject
Create a dataframe from excel using pandas
Create a web app that converts PDF to text using Flask and PyPDF2