因为中国队赢了,再撸一下DJANGO的官方文档吧
对比一下,CBVS和FBVS,哪个方便?
from django.shortcuts import render
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from .models import Question, Choice
from django.template import RequestContext, loader
from django.core.urlresolvers import reverse
from django.views import generic
# Create your views here.
def index(request):
latest_question_list = Question.objects.order_by('pub_date')[:5]
#template = loader.get_template('polls/index.html')
#context = RequestContext(request, {'latest_question_list': latest_question_list})
#output = ','.join([p.question_text for p in latest_question_list])
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
'''
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
'''
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
def vote(request, question_id):
p = get_object_or_404(Question, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except(KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question': p,
'error_message': "You did not select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultView(generic.DetailView):
model = Question
template_name = 'polls/results.html'

因为中国队赢了,再撸一下DJANGO的官方文档吧的更多相关文章
- 【翻译】Django Channels 官方文档 -- Tutorial
Django Channels 官方文档 https://channels.readthedocs.io/en/latest/index.html 前言: 最近课程设计需要用到 WebSocket,而 ...
- 喜大普奔!Django官方文档终于出中文版了
喜大普奔!Django官方文档终于出中文版了 文章来源:企鹅号 - Crossin的编程教室 昨天经 Sur 同学告知才发现,Django 官方文档居然支持中文了! 之所以让我觉得惊喜与意外,是因为: ...
- Django官方文档学习1——第一个helloworld页面
Django 1.10官方文档:https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 1.查看django版本 python -m djan ...
- Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)
Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...
- Django 2.0官方文档中文 总索引
Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本: ...
- django官方文档--对静态文件的管理
一.入门级理解: 在django中对静态文件的管理和模板(template)的思路是一样的.在模板的管理中django是把app用到 到的模板都保存到app目录下的templates子目录中. 静态文 ...
- django官方文档读书笔记
写在前面:这算是第二次读英文原文文档,第一次是读scrapy,感觉还是要做笔记,好记性不如烂笔头,现在已经忘了scrapy文档讲了什么了,心疼.以后要多读多写 经过半年的基础学习(懒,拖延)终于来到w ...
- [转自官方文档] Django——render()
每个视图都需要做2件事,返回一个包含被请求页面内容的HttpResponse对象或者一个404 快捷函数 render( 请求, 模板, 内容) 载入模板,填充上下文,再返回它生成的HttpResp ...
- 巩固复习(Django最基础的部分_具体查看官方文档)
Django学习路1 1.脚本不能随便运行,没准 linux 运行完就上不去了 2.pip 在 linux 上 写 pip3 同理 python 写为 python3 3.在 pycharm 上安装库 ...
随机推荐
- bootstrap整理-1
基本的HTML模板 小伙伴们,上一小节的视频课程已经对Bootstrap提供的模板做了详细的介绍,在这一小节中我们把这个模板代码粘贴过来,以供你们学习查看,你们可以根据实际情况在此基础上进行扩展,只需 ...
- Linux软件安装与卸载
一. 了解Linux应用软件安装包: 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rp ...
- js中的对象封装
用json封装,用构造函数封装,用原型函数进行封装,原型函数最节省内存,将大部分相同的属性或方法放在静态区域中,创建对象时,如果对象属性与默认的不一样,则可以在该对象中覆盖静态区域中的值,不影响其他对 ...
- ios paper for facebook 使用第三方库
facebook paper使用的第三方库 Facebook Paper使用的第三方库 第三方库名 简介 链接 ACE code editor https://github.com/ajaxorg/a ...
- java 知识 链接
www.java2blog.com/2016/04/hashmap-in-java-with-examples.html HashMap全剖析 http://card.weibo.com/articl ...
- Python入门 学习笔记 (二)
今天学习了一些简单的语法规则,话不多说,开始了: 二.数据类型 常用数据类型中的整形和浮点型就不多说了. 1.字符串 ...
- 如何在Angular2中使用Forms
在Angular2中形成两个基本对象是Control和ControlGroup. 控制用户输入 Control 一个控制表示一个输入字段(ngFormControl) 一个控制可以绑定到一个input ...
- Mysql JDBC 连接串参数说明
MySQL的 JDBC URL 格式 for Connector/J 如下例: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][= ...
- MySQL的环境变量
MySQL的环境变量 服务器变量:调整MySQL的工作属性,由MySQL的配置文件决定 状态变量:MySQL运行以后所输出的自身统计信息 在Linux下查看MySQL的环境变量 1.获取MySQL客户 ...
- ArrayList 转换为DataTable
, }; ArrayList list = new ArrayList(arr); var dt = new DataTable(); DataColumn dc = new DataColumn(& ...