博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
因为中国队赢了,再撸一下DJANGO的官方文档吧
阅读量:6323 次
发布时间:2019-06-22

本文共 2179 字,大约阅读时间需要 7 分钟。

对比一下,CBVS和FBVS,哪个方便?

from django.shortcuts import renderfrom django.http import Http404from django.shortcuts import get_object_or_404from django.http import HttpResponse, HttpResponseRedirectfrom .models import Question, Choicefrom django.template import RequestContext, loaderfrom django.core.urlresolvers import reversefrom 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'

转载地址:http://sxlaa.baihongyu.com/

你可能感兴趣的文章
JavaScript数字精度丢失问题总结
查看>>
【转】Java生成对应字符串的MD5密码模块
查看>>
Codeforces Round #335 (Div. 1) C. Freelancer's Dreams 计算几何
查看>>
理解oracle中连接和会话
查看>>
JS与C#编码解码
查看>>
Eclipse添加和查看书签
查看>>
苹果开发者认证 步骤
查看>>
cocos2dx的模板容器简单使用(Vector,Map,Value)
查看>>
python 学习笔记 if语句
查看>>
UVA12130 Summits(BFS + 贪心)
查看>>
uva 11475 - Extend to Palindrome(KMP)
查看>>
微信第三方登录接口
查看>>
用dtree实现树形菜单 dtree使用说明
查看>>
[实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表
查看>>
VNC Centos 2
查看>>
Netty之Java堆外内存扫盲贴
查看>>
跨境电商如何通过腾讯财付通将支付信息发往海关?
查看>>
Android SDK开发包国内下载地址
查看>>
联想S720/S720i通刷刷机包 Vibe V1.0
查看>>
java异常 之 异常的层次结构
查看>>