博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django 应用开发(3)
阅读量:4594 次
发布时间:2019-06-09

本文共 1337 字,大约阅读时间需要 4 分钟。

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
[0-9]+)/$', views.detail, name='detail'),10 # ex: /polls/5/results/11 url(r'^(?P
[0-9]+)/results/$', views.results, name='results'),12 # ex: /polls/5/vote/13 url(r'^(?P
[0-9]+)/vote/$', views.vote, name='vote'),14 ]

 

转载于:https://www.cnblogs.com/fjl-vxee/p/6804324.html

你可能感兴趣的文章
MyEclipse如何调试
查看>>
Java_Set用法总结
查看>>
Codeforces Round #160 (Div. 2) D. Maxim and Restaurant(DP)
查看>>
Exchange Port
查看>>
MoonStack
查看>>
Error parsing 'file:///media/RHEL_5.5\\ x86_64\\ DVD/Server'
查看>>
oracle 01578
查看>>
在source中查看代码
查看>>
pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple
查看>>
apache环境下配置多个ssl证书搭建多个站点
查看>>
PHPExcel随笔
查看>>
利用hadoop自带程序运行wordcount
查看>>
语音活性检测器py-webrtcvad安装使用
查看>>
gson小练习之嵌套复杂数据解析
查看>>
WIFI驱动的移植 realtek 8188
查看>>
Swift - 懒加载(lazy initialization)
查看>>
iOS - 国际化语言切换
查看>>
一张图理解prototype、proto和constructor的三角关系
查看>>
python lambda简单介绍
查看>>
StringBuilder的使用与总结
查看>>