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:// ...
随机推荐
- C#基础-面向对象-多态
多态,不同对象对同一方法的不同实现 使用abstract关键字表示抽象类 // 表示是一个抽象类 public abstract class Animal { private string name; ...
- linux mysql5.7 安装、 开机启动
一.安装 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz h ...
- Java高并发之同步异步
1.概念理解: 2.同步的解决方案: 1).基于代码 synchronized 关键字 修饰普通方法:作用于当前实例加锁,进入同步代码前要获得当前实例的锁. 修饰静态方法:作用于当前类对象加锁,进入同 ...
- axios进行ajax请求得不到数据,cookie无法携带问题
这个坑也是很早之前踩过,今天做项目的时候居然忘了,怎么都拿不到数据,果然好记性不如烂笔头,决定写篇博客来祭奠下我的猪脑子: 原因可能就是你发送请求的时候,需要设置cookie,然而你的cookie并没 ...
- (转)Clang 比 GCC 编译器好在哪里?
编译速度更快.编译产出更小.出错提示更友好.尤其是在比较极端的情况下.两年多前曾经写过一个Scheme解释器,词法分析和语法解析部分大约2000行,用的是Boost.Spirit--一个重度依赖C++ ...
- tcl之控制流-switch
- PHP 基础知识总结
PHP 代表 PHP: Hypertext Preprocessor PHP 文件可包含文本.HTML.JavaScript代码和 PHP 代码 PHP 代码在服务器上执行,结果以纯 HTML 形式返 ...
- CF797E. Array Queries
a is an array of n positive integers, all of which are not greater than n. You have to process q que ...
- 1 Django初探
1.理解MTV request 向服务器请求 response发送数据给用户 M:数据库取出数据 T: 模板渲染 V:渲染好的网页返回给用户 URL找到特定的views 2.创建django项目 (1 ...
- python语法root=Tkinter.Tk()
1. Tkinter 是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口.Tkinter不是唯一的python图形编程接口,但是是 其中比较流行的一个.最大的特点是 ...