[PYTHON] Hello World (beginners) on Django

Introduction

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.

Install django command

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

Create a Django project

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

Add application

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

Addition to settings.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',
]

Routing settings

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),
]

view settings

Create a ʻindex () function in hello / views.pyas you set inhello / 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')

migration

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.

Boot on localhost

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. スクリーンショット 2020-08-10 23.08.30.png That's it.

Summary

--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.

at the end

The project created this time has been released on GitHub.

Recommended Posts

Hello World (beginners) on Django
Hello world
Python #Hello World for super beginners
How to build Hello, World on #Nix
Hello world instead of localhost in Django
(For myself) Django_1 (Basic / Hello World / Template)
cython hello world
Hello World with nginx + uwsgi + python on EC2
Flask Hello World cannot be displayed on VPS
Create a one-file hello world application with django
What Python beginners got hooked on with Django
To myself as a Django beginner (3)-Hello world! ---
Hello X3DOM on Jupyter
web2py memo: Hello World
hello world with ctypes
RabbitMQ Tutorial 1 ("Hello World!")
Hello, World with Docker
Django's first Hello World
Hello world with flask
Draw hello world with mod_wsgi
Django beginners create simple apps 3
Django beginners create simple apps 1
Until hello world with zappa
[Django] Notes on using django-debug-toolbar
Programming language in "Hello World"
Django environment development on Windows 10
Django beginners create simple apps 2
Hello World in GO language
Install Django on your Mac
Django beginners make simple apps 4
Python starting with Hello world!
Django beginners create simple apps 5
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Python beginners tried Hello World in 30 seconds using the micro-framework Flask
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Let's do "Hello World" in 40 languages! !!
Hello, world! With virtual CAN communication
Introduction to TensorFlow --Hello World Edition
Hello world! (Minimum Viable Block Chain)
Django beginners tried building an environment
[For beginners] Django -Development environment construction-
Deploy your Django application on Heroku
python + django + scikit-learn + mecab (1) on heroku
Implement a Django app on Hy
python + django + scikit-learn + mecab (2) on heroku
[Note] Hello world output with python
Publish DJango page on heroku: Practice
cout << "Hello, World! \ N" in python
Hello World in Flask [Appropriate memo]
Code: 2 "Hello World" in "Choregraphe-Python script"
Hello World! By QPython with Braincrash
Update applications running on Django 1.7 to Django 1.8
[Note] Run Django on Amazon Linux 2
Django blog on heroku: login implementation
Introduction to Ansible Part 1'Hello World !!'