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. linux系统环境搭建

    一.安装jdk 参考帖子 用yum安装JDK(CentOS) 1.查看yum库中都有哪些jdk版本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [r ...

  2. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  3. 如何使用Gradle的maven-publish将jar包或者war包上传到nexus仓库

    首先,在build.gradle里边声明依赖maven-publish插件: apply plugin: 'maven-publish' 然后,配置项目的信息和和nexus的信息: publishin ...

  4. 【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理

    [BZOJ5133][CodePlus2017年12月]白金元首与独舞 题面:www.lydsy.com/JudgeOnline/upload/201712/div1.pdf 题解:由于k很小,考虑用 ...

  5. IOS开发 REST请求 ASIHTTPRequest用法

    ASIHTTPRequest类库简介和使用说明 官方网站: http://allseeing-i.com/ASIHTTPRequest/ .可以从上面下载到最新源码,以及获取到相关的资料. 使用iOS ...

  6. spring配置多视图解析器

    最近做一个小项目(移动端),自己搭了个简单的SSM框架(spring + spring MVC + Mybitis),展示层本来选用的是jsp,各方便都已经搭建好,结果发现有些页面需要用到H5的一些功 ...

  7. yii---对数组进行分页

    很多时候,我们会对多个数据进行分页处理,例如我最近开发的一个功能,系统消息,系统消息的来源是多个表,而且多个表之间的数据没有任何关联,这个时候,需要对多个表进行查询,查询返回的数据进行分页,而且采用的 ...

  8. Spark2 文件处理和jar包执行

    上传数据文件 mkdir -p data/ml/ hadoop fs -mkdir -p /datafile/wangxiao/ hadoop fs -ls / hadoop fs -put /hom ...

  9. 关于JavaScript转义字符('、 " 、\" 、\')【原创】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  10. Hibernate的10个常见面试问题及答案

    在Java J2EE方面进行面试时,常被问起的Hibernate面试问题,大多都是针对基于Web的企业级应用开发者的角色的.Hibernate框架在Java界的成功和高度的可接受性使得它成为了Java ...