code: https://github.com/lannyMa/toupiao

polls app介绍

这个例子来源于django官网,恰好2.x版本有中文版. https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial01/

功能介绍

  • 首页

  • 投票

  • 投票结果页

从首页点选项,进入投票(detail)页, 选择-vote(result),跳转到投票页重新投票

代码里值得学习的

1.取出关联表中的数据

detail.html

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

2: url的redirect和reverse

from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from app01.models import Question, Choice
from django.urls import reverse path('specifics/<int:question_id>/', views.detail, name='detail'), <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
return HttpResponseRedirect(reverse('app02:results', args=(question.id,)))

取出所有和取出某一个

https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial04/

关于时间

class Question(models.Model):
question_text = models.CharField(max_length=50)
pub_date = models.DateTimeField(timezone.now()) def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1) def __str__(self):
return self.question_text class Choice(models.Model):
question_text = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=50)
votes = models.IntegerField(default=0) def __str__(self):
return self.choice_text
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())

关于get_object_or_404

https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial03/

detail

from django.http import Http404
from django.shortcuts import render from .models import Question
# ...
def detail(request, 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})
from django.shortcuts import get_object_or_404, render

from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})

[django实践]投票app的更多相关文章

  1. django 添加comments app

    django 添加comments app 参看 django comments 文档 安装和配置comments 1.安装comments,运行:pip install django-contrib ...

  2. django根据不同app配置相应的log文件

    django根据不同app配置相应的log文件 settings.py # django logging LOG_PATH = "/var/log/blog/" LOGGING = ...

  3. Django中的APP

    3. Django中的APP: 什么是APP?以及为什么要用APP? project --> 项目 (老男孩教育大学校) APP --> 应用 (Linux学院/Python学院/大数据学 ...

  4. Django 开发投票系统

    主要参考官方文档 Windows  10 Python 23.5 Django 1.9 1.创建项目(mysite)与应用(polls) D:\python>django-admin.py st ...

  5. django常用第三方app大全

    djangoapp 资源大全 最近经常在这个版面看到Django相关扩展的介绍,而其一个扩展写一个帖子,觉得没太必要吧. 以前整理的django资源列表,从我的wiki上转过来的. 要找django资 ...

  6. Django~NewProject and APP

    New Project 1.新建 django-admin startproject mysite 2.运行 manage.py runserver 8080 New APP 1.manage.py ...

  7. Django 加载 app 中的urls

    在 blog app 下创建 urls.py, 定义该 app 下自有的 url : new/story from blog import views from django.conf import ...

  8. Django --- Django下载和APP创建 ORM (大概步骤)

    1,下载: 命令行: pip install django == 1.11.15 pip install -i或 源 django == 1.11.15 pycharm settings 解释器 点 ...

  9. Django:同一个app支持多个数据库

    我以我个人的Mynote工程说明,目的是要在backend这个app里面设置不同的model对应daysn和bear两个数据库进行操作 现在我们先简单对一个完全新建的django工程配置一个自动在my ...

随机推荐

  1. HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP错误解决方法

    在树莓派上运行在windows上正确的程序,  报错: HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP OpenCV Error: Assertion failed (s ...

  2. echarts.js多图表数据展示使用小结

    echarts api文档: http://echarts.baidu.com/echarts2/doc/doc.html echarts demo示例: http://echarts.baidu.c ...

  3. SeaJS之use函数

    有了 define 等模块定义规范的实现,我们可以开发出很多模块.但光有一堆模块不管用,我们还得让它们能跑起来.在 SeaJS 里,要启动模块系统很简单: <script src=”path/t ...

  4. Qt编写带频谱的音乐播放器

    之前有个项目需要将音频文件的频谱显示出来,想了很多办法,后面发现fmod这个好东西,还是跨平台的,就一个头文件+一个库文件就行,简单小巧功能强大,人家做的真牛逼.为了不卡住界面,采用了多线程处理. 可 ...

  5. HDFS的客户端操作

    命令行操作: -help      功能:输出这个命令参数手册 -ls                   功能:显示目录信息 示例: hadoop fs -ls hdfs://hadoop-serv ...

  6. 让人一看就懂的excel相对引用和绝对引用案例解析

    http://www.ittribalwo.com/article/2831.html 内容提要:本文的excel相对引用和绝对引用.混合引用的使用方法案例截选自<Excel效率手册 早做完,不 ...

  7. ARC下带CF前缀的类型与OC类型转换

    在对钥匙串操作时这个函数 OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef * __nullable CF_RETURNS_R ...

  8. 九度OJ小结

    1. 高精度问题 可参考题目 题目1137:浮点数加法   http://ac.jobdu.com/problem.php?pid=1137 对于高精度问题可以考虑使用结构体.上述为浮点数加法,因此该 ...

  9. 2-2 vue环境搭建以及vue-cli使用

    一.vue多页面应用文件引用 1.官网拷贝: <script src="https://cdn.jsdelivr.net/npm/vue"></script> ...

  10. XmlSerializer的GenerateTempAssembly性能问题例外

    XmlSerializer的两个构造函数不会出现每次构造都创建TempAssembly的性能问题,其内部做了缓存. public XmlSerializer(Type type) public Xml ...