url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
修改 urls.py
其中
(r'^$', 'polls.helloworld.index'),
r’^$’ 是为了匹配空串 ,就是http://localhost:8000/
r’^$’, ‘polls.helloworld.index’),就是说 http://localhost:8000/ 这个地址将会指向
polls 这个工程里 helloworld.py 文件里定义的index方法,看看helloworld.py里面是不是有个def index(request):
举个例子,如果以后要想让http://localhost:8000/ blog 能被访问,只需要加个
r’^blog’ ,然后在后面写views来控制结果(这个以后会讲),够方便吧!
如果此时 web server已经启动,直接刷新页面就可以看到 hello world 了
很方便,django的url配置helloworld就是这样