This is ** 4th **, a memorandum of making a game record management app for shogi using Django.
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
--URL setting --View settings --Create Template
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
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!
Next, specify the detailed URL in the kifu_app application.
Create urls.py in kifu_app.
What you do with this urls.py 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 / *.
Next, create a method to be executed on the server side when displaying the Template (screen). This allows you to view the data dynamically.
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.
Finally, we will create the actual screen (Template).
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!
[Pass DB data to Template] 2