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 urlfrom.import viewsurlpatterns =[# 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 importHttpResponsefrom.models importQuestiondef 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 importHttpResponsefrom django.template import loaderfrom.models importQuestiondef 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 renderfrom.models importQuestiondef 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 importHttp404from django.shortcuts import renderfrom.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, renderfrom.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 tagurl(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 urlfrom.import viewsapp_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:// ...
随机推荐
- Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程
一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...
- Linux关闭开启防火墙命令
在外部访问CentOS中部署应用时,需要关闭防火墙. 关闭防火墙命令:systemctl stop firewalld.service 开启防火墙:systemctl start firewalld. ...
- 连接mysql 报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
网上找不到 朋友说是因为非正常关机导致,mysql.server start 运行报错 ERROR! The server quit without updating PID file(): 解决办 ...
- JZOJ 5914. 盟主的忧虑
Description 江湖由 N 个门派(2≤N≤100,000,编号从 1 到 N)组成,这些门派之间有 N-1 条小道将他们连接起来,每条道路都以“尺”为单位去计量,武林盟主发现任何两个 ...
- MOS管学习笔记
最近在做一个小的电路设计项目,其中遇到了MOS管,经过查询资料,多年遗忘的数电.模电渐渐又浮现在我的脑海,在百度文库找到一篇比较不错的文章,把它截图使用出来,如原稿作者看到感觉侵权,请及时联系我,以便 ...
- [Codeforces958A2]Death Stars (medium)(字符串+hash)
Description 题目链接 Solution 这里用类似hash的方法将判断2个矩阵是否相同的时间降为O(m),总时间复杂度为O(m3) Code #include <cstdio> ...
- python基础之生成器、三元表达式、列表生成式、生成器表达式
生成器 生成器函数:函数体内包含有yield关键字,该函数执行的结果是生成器,生成器在本质上就是迭代器. def foo(): print('first------>') yield 1 pri ...
- Oozie是什么
Apache Oozie Workflow Scheduler for Hadoop Oozie is a workflow scheduler system to manage Apache Had ...
- TouTiao开源项目 分析笔记7 加载数据的过程
1.以新闻页中的段子数据显示为例 1.1.首先执行InitApp==>SplashActivity. 因为在AndroidManifest.xml中定义了一个<intent-filter& ...
- 算法学习记录-图——应用之关键路径(Critical Path)
之前我们介绍过,在一个工程中我们关心两个问题: (1)工程是否顺利进行 (2)整个工程最短时间. 之前我们优先关心的是顶点(AOV),同样我们也可以优先关心边(同理有AOE).(Activity On ...