可识别的视图需满足一下两个条件:
1.第一个参数的类型:HttpRequest
2.返回HttpResponse实例
在新建app的views当中写下以下内容
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
修改urls
from django.conf.urls import patterns, include, url
from django.contrib import admin
from myLesson.views import *
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myTest.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^hello/$',hello),
url(r'^admin/', include(admin.site.urls)),
)
然后打开
localhost:8000/hello
回顾
回顾一下整个流程
1.首先访问settings,当中的ROOT_URLCONF = 'myTest.urls'
2.在urls当中找到(r'^hello/$',hello),
3.在myLesson当中的views当中找到hello
扩展
修改urls,添加
(r'^hello/(d+)/$',hello1),
在views当中添加一个新的函数
def hello1(request,num):
try:
num=int(num)
except Exception as e:
raise Http404()
然后在localhost:8000/hello/3