九.
十.
当一个请求到达服务器时,服务器先去project下的urls.py,再被里面的内容导向myApp下的urls.py, 再被这里面的内容,导向myApp下的views.py
十一.
当前项目的树形结构
[root@VM_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')),
]