一 視圖html
1:概述:django的視圖是指對請求進行迴應,便是一個函數,對應url請求。在view.py中定義。python
2: 如何定義:數據庫
from django.http import HttpResponse def index(request): return HttpResponse('hello python!!!!')
二 url
1:修改urls.pydjango
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('myApp.urls')) ]
2:在app目錄下新建urls.py, 進行以下設置app
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index) ]
而後訪問http://127.0.0.1:8000/,頁面即顯示hello python!!!函數
三 模版url
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')],
四 視圖,模版,url聯合使用顯示一個數據庫表
1:寫grades。html頁面code
{% for grade in grades %} <li> <a href="#"> {{ grade.ganme }} </a> </li> {% endfor %}
2:定義grades 對應的視圖htm
from .models import Grades def grades(request): gradeslist = Grades.objects.all() return render(request, 'myApp/grades.html', {'grades':gradeslist})
3:配置grades對應的url
url(r'^grades/$', views.grades)get
4: 訪問http://127.0.0.1:8000/grades即顯示班級信息。