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
PowerShell
#Create virtual environment
python -m venv venv
#Virtual environment activation
./venv/Script/activate
#Django installation
pip install django
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> .
PowerShell
python manage.py startapp <Application Name>
PowerShell
# localhost:Start the development server on 8000
python manage.py runserver [<Port>]
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.
PowerShell
python manage.py collectstatic
The static files are copied to the directory defined in STATIC_ROOT in \
PowerShell
python manage.py makemigrations
Create a migration file that describes the changes to the database.
PowerShell
python manage.py migrate
Reflect the contents of the migration file in the database.
<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
--When creating directly under the project directory (* 1) --When creating directly under each application directory (* 2)
--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
settings.py
-- LANGUAGE_CODE = "en-us"
++ LANGUAGE_CODE = "ja"
-- TIME_ZONE = "UTC"
++ TIME_ZONE = "Asia/Tokyo"
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>",
++ }
++ }
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.
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.
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.
When adding the routing to "https: // <example.jp> / application /", add to the project routing file and create or add the application routing file.
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.
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