[PYTHON] Django Project Baseline

Create directory & build virtual environment in Terminal

Project root directory

terminal@Appropriate directory


mkdir {project-name}
cd {project-name}

Virtual environment

Create

terminal@root-dir


python -m venv {env-name}
Start-up

terminal(ps)


{env-name}\Scripts\activate

or

terminal(bash)


source {env-name}/bin/activate



Once you've done this Work by opening the project file and terminal in an editor




Create Django projects and apps

Create Django Project

terminal


python -m pip install --upgrade pip
python -m pip install Django
django-admin.exe startproject {project-name} .

Application creation

terminal


python manage.py startapp {app-name} .



from here, The main part of the baseline including environment variable management by .env and isolation by environment of setting.py.




Main story

Added static and template directories directly under the Application directory

Create urls.py directly under Application directory

Create

{app-dir}/urls.py


from django.urls import path, include
from django.conf.urls import url

from . import views


app_name = '{app-name}'

urlpatterns = [
    path('', views.index, name='index'),  #index handler is views.Suppose it is defined in py
]
The routing managed by app-dir is called by project-dir.

{project-dir}/urls.py


from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView


urlpatterns = [
    path('admin/', admin.site.urls),
    path("", include("fit.urls")),
    path('sw.js', (TemplateView.as_view(template_name="book_manager/sw.js", content_type='application/javascript', )), name='sw.js'),
]

Handler definition in views.py

views.py


from django.shortcuts import render


def index(request):
    return render(request, "fit/index.html", {
        "key": "value",
    })

Settings in Settings.py using .env

install django-dotenv

terminal


python -m pip install django-dotenv
.env creation

root/.env


SECRET_KEY = {Copyed secret key}
DJANGO_SETTINGS_MODULE = fitmod.settings.local  #Local(settings will be separated later)
Modification of settings.py (only the changed / added part is described)

settings.py


from dotenv import load_dotenv

# loading env-vars
load_dotenv('.env')

SECRET_KEY = os.getenv("SECRET_KEY")

# ~Abbreviation~

INSTALLED_APPS = [
    ...
    '{app-name}', 
]

# ~Abbreviation~

LANGUAGE_CODE = "ja"

TIME_ZONE = "Asia/Tokyo"

Isolation of settings.py

Use .env to rewrite settings.py loaded by wsgi.py and manage.py

wsgi

root/{project-dir}/wsgi.py


import os

from django.core.wsgi import get_wsgi_application
from dotenv import load_dotenv


load_dotenv('.env')
DJANGO_SETTINGS_MODULE = os.getenv("DJANGO_SETTINGS_MODULE")

os.environ.setdefault('DJANGO_SETTINGS_MODULE', DJANGO_SETTINGS_MODULE)

application = get_wsgi_application()

wsgi

root/manage.py


#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

from dotenv import load_dotenv


load_dotenv('.env')
DJANGO_SETTINGS_MODULE = os.getenv("DJANGO_SETTINGS_MODULE")


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', DJANGO_SETTINGS_MODULE)
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()



The end of the main part of the baseline.




migration

terminal


python manage.py makemigrations
python manage.py migrate

createsuperuser

terminal


python manage.py createsuperuser

Server startup

terminal


python manage.py runserver

Directory structure

{root-dir}/
     │
     ├ {project-name(project-dir)}
     │     ├ settings    #add to
     │    │     ├ init.py
     │    │     ├ local.py
     │    │     └ production.py
     │    ├ init.py
     │    ├ urls.py
     │    ├ asgi.py
     │    ├ wsgi.py
     │     └ db.sqlite3  #When using SQLite
     │
     ├ {app-name(app-dir)}
     │     ├ migrations
     │     ├ template    #add to
     │     │     └ {app-name}
     │     │       └ ...
     │     ├ static      #add to
     │     │    └ {app-name}
     │     │        └ ...
     │    ├ init.py
     │     ├ admin.py
     │     ├ apps.py
     │     ├ model.py
     │     ├ test.py
     │     ├ urls.py     #add to
     │     └ views.py
     │
     ├ venv/
     │   └ ...
     │
     ├ manage.py
     │
     ├ .env              #add to
     ├ .gitignore        #add to
     ├ requirements.txt  #add to(When using heroku)
     ├ Procfile          #add to(When using heroku)
     ├ runtime.txt       #add to(When using heroku)
     └ db.sqlite3  #When using SQLite

.gitignore

root/.gitignore


*.pyc
*~
__pycache__
db.sqlite3
/static
.env
venv

Recommended Posts

Django Project Baseline
Django project environment construction
[Django] Rename the project
Start a Django project
Django
Shell to create django project
django project development environment construction
Deploy django project to heroku
Steps to create a Django project
Register your Django application in your project
django update
Django note 4
Project Euler 37
Project Euler 47
Project Euler 31
Project Euler 4
django search
Project Euler 38
Django installation
Project Euler 26
Project Euler 8
Django Summary
Django test
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 33
Commands for creating a new django project
Project Euler 32
Project Euler 43
Django # 2 (template)
Django Note 5
Touch django
django notes
Django Summary
Project Euler 35
Project Euler 36
Django basics
CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
Project Euler 24
Django Shoho
Project Euler 48
Project Euler 45
Django defaults
Project Euler 6
Project Euler 44
Django + Docker
Project Euler 39
Project Euler 40
Django Glossary
Project Euler 49
Project Euler 29
Project Euler 27
Django search
Project Euler 41
Install Django
Django Project HTTPS Server Settings: Let's Encrypt
Django project structure customization (splitting and structuring)
Django: References
Project Euler 18
Project Euler 13