码迷,mamicode.com
首页 > 其他好文 > 详细

Django 应用开发(3)

时间:2017-05-04 21:59:05      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:bsp   short   png   index   应用开发   rtc   第一个   from   应用   

1.编写第一个视图

打开polls/view.py

技术分享

 

利用一个URLconf将这个视图映射到URL上。

首先先创建一个urls.py文件

技术分享

 

编写polls/urls.py

技术分享

编写mysite/urls.py,让主URLconf可以链接到polls.urls模块。mysite/urls.py中插入一个include()

技术分享

结果:

技术分享

编写更多的视图

polls/view.py

 1 from django.shortcuts import render
 2 from django.http import HttpResponse
 3 
 4 def index(request):
 5     return HttpResponse("Hello,world.You‘re at the polls index.")
 6     
 7 def detail(request,question_id):
 8     return HttpResponse("You ‘ re looking at question %s. " % question_id)
 9     
10 def results(request,question_id):
11     response = "You‘re looking at the results of question %s."
12     return HttpResponse(response % question_id)
13 
14 def vote(request,question_id):
15     return HttpResponse("You‘re voting on question %s." % question_id)
16 
17 # Create your views here.

通过下面的url() 调用将这些新的视图和polls.urls模块关联起来:

 1 from django.conf.urls import url
 2 
 3 from . import views
 4 
 5 urlpatterns = [
 6      # ex: /polls/
 7     url(r^$, views.index, name=index),
 8     # ex: /polls/5/
 9     url(r^(?P<question_id>[0-9]+)/$, views.detail, name=detail),
10     # ex: /polls/5/results/
11     url(r^(?P<question_id>[0-9]+)/results/$, views.results, name=results),
12     # ex: /polls/5/vote/
13     url(r^(?P<question_id>[0-9]+)/vote/$, views.vote, name=vote),
14 ]

 

Django 应用开发(3)

标签:bsp   short   png   index   应用开发   rtc   第一个   from   应用   

原文地址:http://www.cnblogs.com/fjl-vxee/p/6804324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!