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. 【大数据系列】在hadoop2.8.0下配置SecondaryNameNode

    修改namenode上的hdfs-site.xml configuration> <property> <name>dfs.replication</name> ...

  2. key是数字的对象集合

    整理如下: let data = {3: '影视', 4: '音乐', 5: '广场舞', 6: '游戏', 7: '综艺', 8: '动漫', 9: '翻唱', 10: '生活', 11: '美食' ...

  3. Centos 使用 docker

    公司linux服务器基本使用的Centos,以下切换为Centos进行docker的操作. 查看系统版本 [root@Sonar-104 ~]# cat /etc/redhat-release Cen ...

  4. -bash: locate: command not found

    部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum  -y ...

  5. 【CF662C】Binary Table 按位处理

    [CF662C]Binary Table 题意:给你一个$n\times m$的01网格,你可以进行任意次操作,每次操作是将一行或一列的数都取反,问你最多可以得到多少个1? $n\le 20,m\le ...

  6. Node复制文件

    本人开发过程中,经常遇到,要去拷贝模板到当前文件夹,经常要去托文件,为了省事,解决这个问题,写了一个node复制文件. // js/app.js:指定确切的文件名.// js/*.js:某个目录所有后 ...

  7. 单引号、双引号、int和char

    首先说一下C语言中用单引号和双引号的不同(一直搞不清楚): 单引号代表的是一个整数,而这个整数的值是编译器所采用的字符集中的字符序列对应的值.所以一般'A'和ASCII中的65意义相同.对于双引号定义 ...

  8. css 多行文字,超出部分隐藏,...代替

    css虽然简单,但其实也是记得常用的那些,不常用的还是要搜一搜再写

  9. 设置ubuntu默认中文字符

    一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在 /etc/environment中看到:sudo gedit /etc/environment ...

  10. stat命令的实现-mysate

    任务详情 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k,grep -r的使用 伪代码 产品代码mystate.c,提交码云链接 测试代码,mysate与stat( ...