Python学习笔记(Django篇)——4、继续完善视图层
def detail(request, question_id):
returnHttpResponse("You're looking at question %s."% question_id)
def results(request, question_id):
response ="You're looking at the results of question %s."
returnHttpResponse(response % question_id)
def vote(request, question_id):
returnHttpResponse("You're voting on question %s."% question_id)
from django.conf.urls import url
from.import views
urlpatterns =[
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
from django.http importHttpResponse
from.models importQuestion
def index(request):
latest_question_list =Question.objects.order_by('-pub_date')[:5]
output =', '.join([q.question_text for q in latest_question_list])
returnHttpResponse(output)
{%if latest_question_list %}
<ul>
{%for question in latest_question_list %}
<li><a href="/demo/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{%else%}
<p>No polls are available.</p>
{% endif %}
from django.http importHttpResponse
from django.template import loader
from.models importQuestion
def index(request):
latest_question_list =Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('demo/index.html')
context ={
'latest_question_list': latest_question_list,
}
returnHttpResponse(template.render(context, request))

from django.shortcuts import render
from.models importQuestion
def index(request):
latest_question_list =Question.objects.order_by('-pub_date')[:5]
context ={'latest_question_list': latest_question_list}
return render(request,'demo/index.html', context)
from django.http importHttp404
from django.shortcuts import render
from.models importQuestion
# ...
def detail(request, question_id):
try:
question =Question.objects.get(pk=question_id)
exceptQuestion.DoesNotExist:
raiseHttp404("Question does not exist")
return render(request,'demo/detail.html',{'question': question})
from django.shortcuts import get_object_or_404, render
from.models importQuestion
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request,'demo/detail.html',{'question': question})
<h1>{{ question.question_text }}</h1>
<ul>
{%for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

<li><a href="/demo/{{ question.id }}/">{{ question.question_text }}</a></li>
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
...
# the 'name' value as called by the {% url %} template tag
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
...
...
# added the word 'specifics'
url(r'^specifics/(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
...
from django.conf.urls import url
from.import views
app_name ='demo'
urlpatterns =[
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
<li><a href="{% url 'demo:detail' question.id %}">{{ question.question_text }}</a></li>
Python学习笔记(Django篇)——4、继续完善视图层的更多相关文章
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
- Python学习笔记进阶篇——总览
Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...
- Python学习笔记基础篇——总览
Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- Python学习笔记——基础篇【第七周】———类的静态方法 类方法及属性
新式类和经典类的区别 python2.7 新式类——广度优先 经典类——深度优先 python3.0 新式类——广度优先 经典类——广度优先 广度优先才是正常的思维,所以python 3.0中已经修复 ...
- Python 学习笔记---基础篇
1. 简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200 import subprocess cmd="cmd.exe" b ...
- python学习笔记--Django入门一 网页显示时间
我的笔记是学习http://djangobook.py3k.cn/ 课程时做的,这个上边的文章讲的确实是非常的详细,非常感谢你们提供的知识. 上一篇随笔中已经配置好了Django环境,现在继续跟随ht ...
- Python学习笔记——基础篇【第一周】——变量与赋值、用户交互、条件判断、循环控制、数据类型、文本操作
目录 Python第一周笔记 1.学习Python目的 2.Python简史介绍 3.Python3特性 4.Hello World程序 5.变量与赋值 6.用户交互 7.条件判断与缩进 8.循环控制 ...
- Python学习笔记——基础篇【第六周】——面向对象
Python之路,Day6 - 面向对象学习 本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 同时可参考链接: http:// ...
随机推荐
- go get超时解决办法
go get gopkg.in/yaml.v2超时,发现被墙了,解决办法如下: 1.安装golang.org/x/net $ mkdir -p $GOPATH/src/golang.org/x/ $ ...
- Lo、Hi、HiByte、LoWord、HiWord、MakeWord、MakeLong、Int64Rec
本话题会涉及到: Lo.Hi.HiByte.LoWord.HiWord.MakeWord.MakeLong.Int64Rec 譬如有一个 Cardinal 类型的整数: 1144201745其十六进制 ...
- php扩展开发-面向对象
在zval变量里IS_OBJECT类型使用zend_object_value来保存变量的,我们看一下他的具体结果. typedef struct _zend_object_value { zend_o ...
- [Bzoj1034][ZJOI2008]泡泡堂BNB(贪心)
Description 题目链接 Solution 这题就是一个贪心, 如果最弱的能赢对方最弱的就赢 否则最强的能赢对面最强的就赢 否则最弱的换对面最强 Code #include <cstdi ...
- [Bzoj1037][ZJOI2008]生日聚会(DP)
Description 题目链接 Solution 这题状态比较难想, \(dp[i][j][g][h]\)表示强i个人有j个男生,在某个区间男生最多比女生多g人,女生最多比男生多h人的方案数,然后D ...
- 3-1 练习 HTML 总结
1.段落 #段落 <div class="ui segment"> </div> #翻转 <div class="ui inverted s ...
- Python数据类型一
一.整型 在Python内部对整数的处理分为普通整数和长整数,普通整数长度为机器位长,通常都是32位,超过这个范围的整数就自动当长整数处理,而长整数的范围几乎完全没限制Python可以处理任意大小的整 ...
- css一些事儿
1. margin和padding 如果边界画一条线,则margin的属于边界外,padding属于边界内 当我们给元素背景色时,margin区域不会被着色,而padding区域会被着色. 当上下两个 ...
- 什么时候会报unrecognized selector的异常?
当调用该对象上某个方法,而该对象上没有实现这个方法的时候, 可以通过“消息转发”进行解决,如果还是不行就会报unrecognized selector异常 objc是动态语言,每个方法在运行时会被动态 ...
- 关于mysqldump备份非事务表的注意事项
Preface We're used to get a logical backup set(whole instance) by simply specifying "-- ...