English Note / Chinese Note
User types the url and keypress, the request is created
The request will be sent to 'urls.py'
Django will match the url with urlpatterns
data
If the match happend, Django will return the view.
#urls.py
from app import views
from django .urls import path
urlpatterns = [
path ('index/' , views .index , name = 'index' ),
]
#views.py
def index (request ):
...
return render (request ,'index.html' )
What's Virtual Environment
A Virtual Environment which can run python.The pip packages is installed in different environment won't disturb each others.
How to install Virtual Environment
Create a random file
create a Virtual Environment
python3 - m venv djangogirls_venv //
swithch to the Virtual Environment
source djangogirls_venv/bin/ activate
How Django connect to Database?
Database setting in .env
file
DB_CONNECTIONS = mysql
DB_HOST = XXX .XXX .X .XXX
DB_PORT = XXXX
DB_DATABASE = maltose - dev
DB_USERNAME = root
DB_PASSWORD = password
Install package which project need
$ source djangogirls_venv / bin / activate
$ pip install XXXX or
$ pip install - r `requirements/local.txt`
$ python manage . py runserver
用戶在敲下你的網址並回車,生成請求;
請求傳遞到urls.py;
Django的去urlpatterns的中匹配鏈接(Django的會在匹配到的第一個就停下來);
一旦匹配成功,則Django便會給出相應的視圖頁面(該頁面可以為一個的Python的函數,或者基於視圖(Django的內置的)的類),也就是用戶看到的頁面;
#urls.py
from app import views
from django .urls import path
urlpatterns = [
path ('index/' , views .index , name = 'index' ),
]
#views.py
def index (request ):
...
return render (request ,'index.html' )
一個可以 Run python 的虛擬環境,可以在裡面安裝各種套件,然後不同虛擬環境不會互相影響.
python3 - m venv djangogirls_venv //
source djangogirls_venv/bin/ activate
DB_CONNECTIONS = mysql
DB_HOST = XXX .XXX .X .XXX
DB_PORT = XXXX
DB_DATABASE = maltose - dev
DB_USERNAME = root
DB_PASSWORD = password
$ source djangogirls_venv / bin / activate
$ pip install XXXX or
$ pip install - r `requirements/local.txt`
$ python manage . py runserver