It's a Shiba Inu Ponta who is hot and hard to walk. Today, when I jumped into the river and took a bath while taking a walk, the owner hated me.
Now, I want to make a Django application.
(venv_dog) Ponta@shiba_app # tree
.
├── db.sqlite3
├── manage.py
└── shiba_app
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ ├── settings.cpython-38.pyc
│ ├── urls.cpython-38.pyc
│ └── wsgi.cpython-38.pyc
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 11 files
I'll start here today. Well, there are more directories (\ _ \ _ pycache \ _ \ _) and files. I'm not sure, so I'm relieved for the time being. Create an application wan. The command is "python manage.py startalp (application name)" is.
(venv_dog) Ponta@shiba_app # python manage.py startapp wan
(venv_dog) Ponta@shiba_app # tree
.
├── db.sqlite3
├── manage.py
├── shiba_app
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── wan
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
4 directories, 18 files
You now have a directory wan and a file in it. Next, let's display something. I will write the contents to be displayed in views.py, but before that, I will take a look at views.py.
(venv_dog) Ponta@shiba_app # cat wan/views.py
from django.shortcuts import render
# Create your views here.
(venv_dog) Ponta@shiba_app #
I wrote as follows.
(venv_dog) Ponta@shiba_app # cat wan/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Wan!Shiba Inu Ponta")
(venv_dog) Ponta@shiba_app #
Now, let's set this function to be called by http access. You can call it directly from shiba_app / urls.py, but since I made an application called wan, I would like to call it from wan / urls.py.
(venv_dog) Ponta@shiba_app # cat shiba_app/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('wan/', include('wan.urls')),
]
(venv_dog) Ponta@shiba_app # cat wan/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
(venv_dog) Ponta@shiba_app #
This is OK. http://127.0.0.1:8000/wan When you access, the second line of urlpatterns in shiba_app / urls.py, path('wan/', include('wan.urls')) Transferred to the application wan directory wan, then In urlpatterns in wan / urls.py path('', views.index, name='index') The function index described in views.py should be called.
Let's do it. First, start the test server.
(venv_dog) Ponta@shiba_app # python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 23, 2020 - 08:59:15
Django version 3.1, using settings 'shiba_app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Go to http://127.0.0.1:8000/wan/ in your browser.
One displayed properly! It's a success! By the way, in the terminal, it is displayed as follows,
[23/Aug/2020 09:15:36] "GET /wan/ HTTP/1.1" 200 20
HTTP status code 200 (OK) is returned. See you later! Bye bye!
Recommended Posts