对比一下,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. iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

    先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...

  2. Linux软件安装与卸载

    一. 了解Linux应用软件安装包: 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rp ...

  3. c#面向对象编程基础

    1.  为什么要有面向对象? (1)       增加代码重用. (2)降低维护负担,将具备独特性质的代码封装起来,修改程序时,相互不会影响. 2.数据封装用来解决全局变量不易维护的问题. 3.多态: ...

  4. 20160501--struts2入门3

    一.自定义拦截器 要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: public class PermissionInterc ...

  5. C# 的static与单例模式

    C# 的static与单例模式 static是静态对象,在类被第一次使用,或者第一次被实例化时执行 /// <summary> /// 线程安全的单件模式 /// </summary ...

  6. 500 OOPS: cannot change directory:/home/test

    问题:  以root   从远程客户端 登录 FTP  一直密码错误.  发现不能以root 登录, 需要创建其它的用户. 创建一个test 用户后(如下): useradd test; passwd ...

  7. CentOS 7 安装 Apache PHP MariaDB

    准备篇: 一.配置防火墙,开启80端口.3306端口 CentOS 7 默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl sto ...

  8. AVAudioSession 的 AVAudioSessionCategory 和 AVAudioSessionCategoryOptions 相关

    AVAudioSessionCategory相关 AVAudioSessionCategoryAmbient 使用这个category的应用会随着静音键和屏幕关闭而静音.并且不会中止其它应用播放声音, ...

  9. 我爆一个托 QQ305242038 电话 18782169971

    这是两个人,一品天下附近的托,qq负责聊天,电话那个负责见面

  10. IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull

    1.oc最好 用nil   [ nil  任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...