terminal@Appropriate directory
mkdir {project-name}
cd {project-name}
root-dir
described below refers to the {project-name} directory created here.terminal@root-dir
python -m venv {env-name}
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
terminal
python -m pip install --upgrade pip
python -m pip install Django
django-admin.exe startproject {project-name} .
{project-dir}
described below refers to the directory created here.terminal
python manage.py startapp {app-name} .
{app-dir}
described below refers to the directory created here.from here, The main part of the baseline including environment variable management by .env and isolation by environment of setting.py.
{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
]
{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'),
]
views.py
from django.shortcuts import render
def index(request):
return render(request, "fit/index.html", {
"key": "value",
})
) directly under ʻapp-dir / template
in advance.install django-dotenv
terminal
python -m pip install django-dotenv
root/.env
SECRET_KEY = {Copyed secret key}
DJANGO_SETTINGS_MODULE = fitmod.settings.local #Local(settings will be separated later)
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"
root / project-dir / setting
.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
terminal
python manage.py runserver
{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