1. 程式人生 > >《千鋒Django培訓》網課筆記(從9到11)

《千鋒Django培訓》網課筆記(從9到11)

九.

 

 

 

 十. 

 當一個請求到達伺服器時,伺服器先去project下的urls.py,再被裡面的內容導向myApp下的urls.py, 再被這裡面的內容,導向myApp下的views.py

 

 

 

 

 十一.

當前專案的樹形結構

[[email protected]_0_15_centos project]# tree
.
|-- db.sqlite3
|-- manage.py
|-- myApp
|   |-- admin.py
|   |-- apps.py
|   |-- __init__.py
|   |-- migrations
|   |   |-- 0001_initial.py
|   |   |-- __init__.py
|   |   `-- __pycache__
|   |       |-- 0001_initial.cpython-36.pyc
|   |       `-- __init__.cpython-36.pyc
|   |-- models.py
|   |-- __pycache__
|   |   |-- admin.cpython-36.pyc
|   |   |-- __init__.cpython-36.pyc
|   |   |-- models.cpython-36.pyc
|   |   |-- urls.cpython-36.pyc
|   |   `-- views.cpython-36.pyc
|   |-- tests.py
|   |-- urls.py
|   `-- views.py
|-- project
|   |-- __init__.py
|   |-- __pycache__
|   |   |-- __init__.cpython-36.pyc
|   |   |-- settings.cpython-36.pyc
|   |   |-- urls.cpython-36.pyc
|   |   `-- wsgi.cpython-36.pyc
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- templates

 

 

 

 

 

 

 

 

 第一個檔案:/home/virtualenvs/01-sunck/project/myApp/views.py

# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse('This is the main page.')
def detail(request,num):
    return HttpResponse('detail-%s'%num)

from .models import Grades
def grades(request):
    #get data from models
    gradesList = Grades.objects.all()
    #send data to templates, templates render them and send them to the browser.
    return render(request, 'myApp/grades.html',{"grades":gradesList})

from .models import Students
def students(request):
    #get data from models
    studentsList = Students.objects.all()
    #send data to templates, templates render them and send them to the browser.
    return render(request, 'myApp/students.html',{"students":studentsList})

 第二個檔案:/home/virtualenvs/01-sunck/project/myApp/urls.py 

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index),
    path('<int:num>',views.detail),
    path('grades/',views.grades),
    path('students/',views.students),
]

 第三個檔案:/home/virtualenvs/01-sunck/project/project/urls.py 

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('myApp.urls')),
]