[Django3] Environment construction and various settings summary [Python3]

Introduction

This is a summary of CLI commands, directory structure, and various settings required for system development with Django.

Django has a lot of official documentation, so if you have the time, check it out. I have a lot of Japanese! Django Official Documentation

System configuration

Django installation

PowerShell


#Create virtual environment
python -m venv venv

#Virtual environment activation
./venv/Script/activate

#Django installation
pip install django

Django CLI cheat sheet

Project creation

PowerShell


#In the current directory<Project Name>Create a directory and create a project in it
django-admin startproject <Project Name>

#Create a project directly under the current directory
django-admin startproject <Project Name> .

Application creation

PowerShell


python manage.py startapp <Application Name>

Start development server

PowerShell


# localhost:Start the development server on 8000
python manage.py runserver [<Port>]

Create administrative user

PowerShell


python manage.py createsuperuser

After executing the command, you will be prompted to enter your "user name", "email address", and "password". To log in to the management screen, access http: // localhost: 8000 / admin / after starting the development server.

Static file aggregation

PowerShell


python manage.py collectstatic

The static files are copied to the directory defined in STATIC_ROOT in \ /settings.py.

migration

Creation of migration file

PowerShell


python manage.py makemigrations

Create a migration file that describes the changes to the database.

Migration execution

PowerShell


python manage.py migrate

Reflect the contents of the migration file in the database.

Django directory structure

<Project Name> ┬ venv                   #Virtual environment directory
               ├ static                 #Static file storage directory(※3)
               │ ├ css
               │ ├ js
               │ └ ...
               ├ templates              #Template storage directory(※1)
               │ ├ base.html            #Template base file
               │ └ <Application Name>
               │   └ ...                #Template file
               ├ <Project Name>         #Project directory
               │ ├ __init__.py
               │ ├ asgi.py
               │ ├ settings.py          #Project configuration file
               │ ├ urls.py              #Project routing definition file
               │ └ wsgi.py
               ├ <Application Name>     #Application directory
               │ ├ static               #Static file storage directory(※4)
               │ │ ├ css
               │ │ ├ js
               │ │ └ ...
               │ ├ migrations
               │ ├ templates            #Template storage directory(※2)
               │ │ └ ...                #Template file
               │ ├ __init__.py
               │ ├ admin.py             #Management site configuration file
               │ ├ apps.py              #Application configuration configuration file
               │ ├ forms.py             #Form definition file
               │ ├ models.py            #Model definition file
               │ ├ tests.py             #Test code description file
               │ ├ urls.py              #Application routing definition file
               │ └ views.py             #View definition file
               ├ manage.py              #Utility command execution file
               └ requirements.txt       #Package List

templates directory

--When creating directly under the project directory (* 1) --When creating directly under each application directory (* 2)

static directory

--Static directory directly under the project directory (* 3) --Stores static files for the entire project that are not tied to a specific application --Static directory directly under each application directory (* 4) --Stores static files associated with applications

Django project settings

Language and timezone

settings.py


-- LANGUAGE_CODE = "en-us"
++ LANGUAGE_CODE = "ja"

-- TIME_ZONE = "UTC"
++ TIME_ZONE = "Asia/Tokyo"

Database

settings.py


   #For MySQL
-- DATABASES = {
--     "default": {
--         "ENGINE": "django.db.backends.sqlite3",
--         "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
--     }
-- }
++ DATABASES = {
++     "default": {
++         "ENGINE": "django.db.backends.mysql",
++         "HOST": "127.0.0.1",
++         "PORT": "3306",
++         "NAME": "<DB Name>",
++         "USER": "<User Name>",
++         "PASSWORD": "<Password>",
++     }
++ }

Application list

settings.py


   #application When adding an application
   INSTALLED_APPS = [
       "django.contrib.admin",
       "django.contrib.auth",
       "django.contrib.contenttypes",
       "django.contrib.sessions",
       "django.contrib.messages",
       "django.contrib.staticfiles",
++     "application.apps.ApplicationConfig",
   ]

After creating a new application, add the application to the project configuration file.

Static files (CSS, JavaScript, Images, ...)

project

settings.py


   #URL for static file delivery
   STATIC_URL = "/static/"

   #Static file storage directory that is not associated with a specific application
++ STATICFILES_DIRS = (
++     os.path.join(BASE_DIR, "static"),
++ )

   #Directory where static files are aggregated by the collectstatic administration command during deployment
++ STATIC_ROOT = "/var/www/example.com/static/"

Define a static file storage directory in the project configuration file.

template

index.html


++ {% load static %}
   <!DOCTYPE html>
   <html>
     <head>
       ...
++     <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

After loading the static tag at the top of the template file, you can then use the static tag to describe the static file with a relative path.

Django routing

When adding the routing to "https: // <example.jp> / application /", add to the project routing file and create or add the application routing file.

project

settings.py


   TEMPLATES = [
       {
           "BACKEND": "django.template.backends.django.DjangoTemplates",
--         "DIRS": [],
++         "DIRS": [os.path.join(BASE_DIR, "templates")],
           "APP_DIRS": True,
           "OPTIONS": {
               "context_processors": [
                   "django.template.context_processors.debug",
                   "django.template.context_processors.request",
                   "django.contrib.auth.context_processors.auth",
                   "django.contrib.messages.context_processors.messages",
               ],
           },
       },
   ]

The above settings are required when creating a templates directory directly under the project directory. If you want to create a templates directory directly under each application directory, it's okay if "APP_DIRS" is True.

urls.py


-- from django.urls import path
++ from django.urls import path, include

   urlpatterns = [
       path("admin/", admin.site.urls),
++     path("application/", include("application.urls")),
   ]

Describe the routing from the route (/). By using the include function, the routing process can be transferred to the application side.

application

urls.py


++ from django.urls import path
++ from . import views

++ app_name = "application"
++ urlpatterns = [
++     path("", views.IndexViews.as_view(), name="index")
++ ]

The application directory created by the command does not include urls.py, so if you are routing the application for the first time, you will need to create it yourself.

views.py


-- from django.shortcuts import render
++ from django.views import generic

++ class IndexView(generic.TemplateView):
       #When creating a templates directory directly under the application directory
++     template_name = "index.html"   
       #When creating a templates directory directly under the project directory
++     template_name = "<Application Name>/index.html"

Associate the view with the template by storing the relative path of the template file starting from the templates directory in the template_name variable.

Recommended Posts

[Django3] Environment construction and various settings summary [Python3]
Python environment construction and TensorFlow
Environment construction of python and opencv
Django environment construction
Python environment construction
Environment construction (python)
django environment construction
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Python and machine learning environment construction (macOS)
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Python Django tutorial summary
Django project environment construction
python windows environment construction
homebrew python environment construction
Python development environment construction
python2.7 development environment construction
Mac environment construction Python
Python environment construction @ Win7
Summary of python environment settings for myself [mac] [ubuntu]
Web application made with Python3.4 + Django (Part.1 Environment construction)
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Python + Anaconda + Pycharm environment construction
Django development environment construction memo
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Python environment construction For Mac
Anaconda3 python environment construction procedure
Docker + Django + React environment construction
Python3 environment construction (for beginners)
Install Python 3.7 and Django 3.0 (CentOS)
NumPy and matplotlib environment construction
[MEMO] [Development environment construction] Python
django timezone and language settings
django project development environment construction
Environment construction of python2 & 3 (OSX)
Until Python [Django] de Web service is released [Environment construction]
Virtual Environment Version Control Summary Python
Mac + Eclipse (PyDev) + Django environment construction
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Python practice_Virtual environment setup ~ Django installation
Anaconda python environment construction on Windows 10
Python + Unity Reinforcement learning environment construction
[Django] Memorandum of environment construction procedure
I checked Mac Python environment construction
[For beginners] Django -Development environment construction-
Python environment construction memo on Mac
Various Anaconda settings in Windows 10 environment
Python environment construction (pyenv, anaconda, tensorflow)
[Python3] Development environment construction << Windows edition >>
Python development environment construction on macOS
Python environment construction (pyenv + poetry + pipx)
Ml-Agents Release 6 (0.19.0) Environment Construction Summary [Windows]
Emacs settings for Python development environment
Environment construction of python3.8 on mac
Django static file (static) related settings summary
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Python3 TensorFlow for Mac environment construction