project
# installation python3 -m pip install Django # check version python3 -m django --version # start a new project in current dir django-admin startproject <project-name> # run the project in dev mode python3 manage.py runserver # run migrations python3 manage.py migrate
app
modulerun command in project root (manage.py)
# add an app in the same dir as manage.py python3 manage.py startapp <app-name>
You need to add the app as a module in settings.py
:
INSTALLED_APPS = [ ... 'myapp', ]
app
to project
Folder structure:
djangotutorial/ manage.py mysite/ __init__.py settings.py urls.py asgi.py wsgi.py myapp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py urls.py views.py
Link new views
to url
in app:
# myapp/urls.py from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index"), ]
Link the app
to the root project
:
# myproject/urls.py from django.contrib import admin from django.urls import include, path urlpatterns = [ # "polls/" route can be named anything path("polls/", include("myapp.urls")), path("admin/", admin.site.urls), ]
Adding Package as a project dependency:
# add pip package to django app as dependency pip install <package-name> --requirement requirements.txt # OR pip freeze > requirements.txt
Alternatively, add it to definition in settings.py
.
INSTALLED_APPS = [ ... 'package_name', ]