All technological notes.
Create a project directory.
Change the current directory to the project directory
cd <protject_dir>
<protject_dir>: the path of project directory.Create Virtual Environtmet:
python -m venv <env_name>
<env_name>: the name of virtual environment.Activate Virtual Environment:
\project_dir> <env_name>\scripts\activate
CLI:
(env_name)\project_dir> python -m pip install django
Verify:
(env_name)\project_dir> py -m django --version
# the version nubmer will be returned.
Change current directory to project directory
cd project_dir
Create project directory using django-admin command:
(env_name)\project_dir> django-admin startproject <proj_name> .
<proj_name>: the name of project directory.: create the project directory within the current directory.The following files and folder are created:
project_dir/: a container for project
manage.py: a py file associated with many django commands.proj_name/: the actual Python package for the project. Its name is the Python package name to be used to import.
__init__.py: An empty file that tells Python that this directory should be considered a Python package.asgi.py: An entry-point for ASGI-compatible web servers to serve the project.用于处理具有异步功能的标准接口。settings.py: a py file to store all project settingsurls.py: The URL declarations for this Django project;wsgi.py: a py file that acts as the Web Server Gateway Interface for deployment.Verify: Run the django project
(env_name)\project_dir> py manage.py runserver
Change current working directory into project directory <project_dir>, not the django proejct directory <proj_name>
py manage.py startapp <app_name>
<app_name>: the name of applicationThe following folder and files are created:
app_name/: a directory as a Python package for the application.
__init__.py: a py file to indicate that the current folder should be considered a Python package.admin.py: a py file to store models used with Django’s admin interface.apps.py: a py file to store application specific configurationsmodels.py: a py file to store the application’s data modelstests.py: a py file to store test functions to test codeviews.py: a py file to store functions that handle requests and return responsesmigrations/: a directory stores database specific information
__init__.py: a py file to indicate that the current folder should be considered a Python package.<app_name>\view.py:from django.shortcuts import render
from django.http import HttpResponse # import HttpResponse from http module
# create an index function taking request as parameter
def index(request):
return HttpResponse("Hellow world!") # return a HttpResponse object with a string content.
<app_name>\url.py file:from django.urls import path # import path function from urls module
from <app_name> import views # import views module from <app_name> package
# define a list of url patterns
urlpatterns = [
path("", views.index, name="index"),
]
<proj_name>\urls.pyfrom django.contrib import admin
from django.urls import path, include # import path, include function from urls module
urlpatterns = [
path('admin/', admin.site.urls),
path("",include("<app_name>.urls"))
]
(env_name)\project_dir> py manage.py runserver
RUN AND DEBUG > create a launch.json filedebugger for: Pythondebug configuration: DjangoEnter the path to manage.py: default
launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django", // name of launch configuration
"type": "python", // type
"request": "launch",
"program": "${workspaceFolder}\\manage.py", // path to django manage.py file
"args": ["runserver", "--noreload", "5000"], // django command argument
"django": true,
"justMyCode": true
}
]
}
F5 to lauch debug.