I wanted to get a feel for Python, and I used the most popular framework called Django to display Hello World
in the browser, so I'll post a memorandum of it.
I referred to this article this time.
Django's first Hello World https://qiita.com/Yuji_6523/items/d601ad11ad49b9e7ab0e
As a prerequisite, python and pip are already installed.
Run pip install django
to install the django command.
$ python --version
Python 3.8.2
$ pip install django
# (abridgement)
$ python -m django --version
3.1
This time, it is an example of creating a project named helloWorldProject
.
You can create a project by running the command django-admin startproject [project name] [directory to create]
.
$ django-admin startproject helloWorldProject .
$ tree
.
├── helloWorldProject
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Since there is nothing at this point, let's add an application that starts when accessed with domain / hello
and a browser.
This time we will add an application named hello
.
You can add an application by running the command python manage.py startapp [application name]
.
$ python manage.py startapp hello
$ tree
.
├── hello
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── helloWorldProject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── settings.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
If this is left as it is, the hello
application has not been applied, so add the settings.
Add that there is an application called hello
in helloWorldProject / settings.py
.
helloWorldProject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#add to
'hello',
]
Set the routing settings in helloWorldProject / urls.py
.
helloWorldProject/urls.py
from django.contrib import admin
from django.urls import path, include #include added
urlpatterns = [
path('admin/', admin.site.urls),
#add to
path('', include('hello.urls')),
]
You may write path ('hello',
here, but this time, create a file called hello / urls.py
and`` domain / hello for routing. Throw it all at hello / urls.py
.
The reason for this is to aim for a program with high aggregation and low coupling as much as possible.
In addition, create a new hello / urls.py
and set it to call the index () function of hello / views.py
when accessed with domain / hello
.
hello/urls.py(Newly added file)
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index),
]
Create a ʻindex () function in
hello / views.pyas you set in
hello / urls.py`.
This time, the file of Content-Type: text / html
described as Hello World
is returned in the HTTP response.
hello/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
Although it is not so relevant this time, migration here refers to the function that automatically creates and manages the database definition used in the application.
It can be executed with the following command.
$ python manage.py migrate
If the migration is successful, there should be no error after executing the command.
You can start it on the local host with the following command.
$ python manage.py runserver
By default, it starts on port 8000, so if you access http: // localhost: 8000 / hello
and see Hello World
written in python: hello / views.py
, it is successful.
That's it.
--You can now create projects using a framework called Django --You can now add applications in Django --You can now start the application on the local host with Django and check the operation.
The project created this time has been released on GitHub.
Recommended Posts