对比一下,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的官方文档吧的更多相关文章

  1. 【翻译】Django Channels 官方文档 -- Tutorial

    Django Channels 官方文档 https://channels.readthedocs.io/en/latest/index.html 前言: 最近课程设计需要用到 WebSocket,而 ...

  2. 喜大普奔!Django官方文档终于出中文版了

    喜大普奔!Django官方文档终于出中文版了 文章来源:企鹅号 - Crossin的编程教室 昨天经 Sur 同学告知才发现,Django 官方文档居然支持中文了! 之所以让我觉得惊喜与意外,是因为: ...

  3. Django官方文档学习1——第一个helloworld页面

    Django 1.10官方文档:https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 1.查看django版本 python -m djan ...

  4. Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)

    Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...

  5. Django 2.0官方文档中文 总索引

    Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本:  ...

  6. django官方文档--对静态文件的管理

    一.入门级理解: 在django中对静态文件的管理和模板(template)的思路是一样的.在模板的管理中django是把app用到 到的模板都保存到app目录下的templates子目录中. 静态文 ...

  7. django官方文档读书笔记

    写在前面:这算是第二次读英文原文文档,第一次是读scrapy,感觉还是要做笔记,好记性不如烂笔头,现在已经忘了scrapy文档讲了什么了,心疼.以后要多读多写 经过半年的基础学习(懒,拖延)终于来到w ...

  8. [转自官方文档] Django——render()

    每个视图都需要做2件事,返回一个包含被请求页面内容的HttpResponse对象或者一个404 快捷函数  render( 请求, 模板, 内容) 载入模板,填充上下文,再返回它生成的HttpResp ...

  9. 巩固复习(Django最基础的部分_具体查看官方文档)

    Django学习路1 1.脚本不能随便运行,没准 linux 运行完就上不去了 2.pip 在 linux 上 写 pip3 同理 python 写为 python3 3.在 pycharm 上安装库 ...

随机推荐

  1. hibernate缓存技术

    1.缓存 2.Hibernate 缓存作用:为了提高查询效率. 3.第一次操作某个对象的时候,把操作的对象数据存储到缓存中,然后下一次在对同一个对象操作的时候,就不会在连接数据库. 4.Hiberna ...

  2. modelsim脚本文件的编写

    第一章 ModelSim介 绍 本指南是为 ModelSim5.5f版本编写的,该版本运行于UNIX和Microsoft Windows 95/98/Me/NT/2000的操作系统环境中.本指南覆盖了 ...

  3. hihocoder 第一周 最长回文字串

    题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...

  4. primefaces 带参数的组件

    分析了一下primefaces extension showcase的源码,总结一下组件传参的方式. 下面定义了一个组件,名为fourTabsDecoratorCustom.xhtml 1: < ...

  5. ios Toll-Free Bridging

    有一些数据类型是能够在 Core Foundation Framework 和 Foundation Framework 之间交换使用的.这意味着,对于同一个数据类型,你既可以将其作为参数传入 Cor ...

  6. [03] SQL优化

    1.SQL优化的实质 充分利用索引: 访问尽量少的数据块: 减少表扫描的I/O次数: 尽量避免全表扫描和其他额外开销: 2.oracle数据库常用的两种优化器 RBO(rule-based-optim ...

  7. 数据操作So easy-LINQ解析

    1.LINQ是什么? LINQ是Language Integrated Query的缩写,即“语言集成查询”的意思.LINQ的提出就是为了提供一种跨越各种数据源的统一的查询方式,它主要包含4个组件-- ...

  8. OC与Swift的区别一(文件结构)

    1.文件后缀名 oc的文件后缀名为:头文件.h  主体文件.m swift文件后缀名为:.swift 2. 代码分隔符 oc中使用分号;作为代码分隔符 swift中无需使用代码分隔符,以行作为代码分隔 ...

  9. JavaScript 学习笔记-- ES6学习(一)介绍以及Babel的使用

    本文摘自阮一峰老师的<ECMAScript 6入门>,原文地址:http://es6.ruanyifeng.com/#docs/intro ECMAScript 6 是一个泛指,含义是5. ...

  10. 九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)

    题目地址:http://ac.jobdu.com/problem.php?pid=1455 题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元, ...