[django实践]投票app
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的更多相关文章
- django 添加comments app
django 添加comments app 参看 django comments 文档 安装和配置comments 1.安装comments,运行:pip install django-contrib ...
- django根据不同app配置相应的log文件
django根据不同app配置相应的log文件 settings.py # django logging LOG_PATH = "/var/log/blog/" LOGGING = ...
- Django中的APP
3. Django中的APP: 什么是APP?以及为什么要用APP? project --> 项目 (老男孩教育大学校) APP --> 应用 (Linux学院/Python学院/大数据学 ...
- Django 开发投票系统
主要参考官方文档 Windows 10 Python 23.5 Django 1.9 1.创建项目(mysite)与应用(polls) D:\python>django-admin.py st ...
- django常用第三方app大全
djangoapp 资源大全 最近经常在这个版面看到Django相关扩展的介绍,而其一个扩展写一个帖子,觉得没太必要吧. 以前整理的django资源列表,从我的wiki上转过来的. 要找django资 ...
- Django~NewProject and APP
New Project 1.新建 django-admin startproject mysite 2.运行 manage.py runserver 8080 New APP 1.manage.py ...
- Django 加载 app 中的urls
在 blog app 下创建 urls.py, 定义该 app 下自有的 url : new/story from blog import views from django.conf import ...
- Django --- Django下载和APP创建 ORM (大概步骤)
1,下载: 命令行: pip install django == 1.11.15 pip install -i或 源 django == 1.11.15 pycharm settings 解释器 点 ...
- Django:同一个app支持多个数据库
我以我个人的Mynote工程说明,目的是要在backend这个app里面设置不同的model对应daysn和bear两个数据库进行操作 现在我们先简单对一个完全新建的django工程配置一个自动在my ...
随机推荐
- [Win32]一个调试器的实现(五)调试符号
一个调试器应该可以跟踪被调试程序执行到了什么地方,显示下一条将要执行的语句,显示各个变量的值,设置断点,进行单步执行等等,这些功能都需要一个基础设施的支持,那就是调试符号. 什么是调试符号 我们知道, ...
- 《转》python学习(11)-表达式和语句
转自 http://www.cnblogs.com/BeginMan/p/3164600.html 一.Python语句 if语句.else语句.elif语句.条件表达式.while语句.for语句. ...
- JWNL的配置使用
JWNL是什么? JWNL is an API for accessing WordNet-style relational dictionaries. It also provides functi ...
- C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...
- truncate表恢复
Fy_Recover_Data 2014.03.07 更新 -- 现在可以从离线文件中恢复被Truncated的数据了 2014新版本的恢复思路 与 2012的不同 2014年是离线恢复 http:/ ...
- wireshark 表达式备忘录
参考资料: https://blog.csdn.net/wojiaopanpan/article/details/69944970 wireshark分两种表达式,一种是捕获表达式,这个是在捕获时用的 ...
- Sublime text3配置LiveReload 浏览器即时刷新
1.在sublime控件台 install livereload插件(缺点:每次重新打开Sublime都需要启动) 2.配置Preference > Package Settings > ...
- 更新jenkins插件,报错 Perhaps you need to run your container with "-Djava.awt.headless=true"?
Configuring the Java environment variables vi ~/.bash_profile 在最后一行加入: export JAVA_OPTS=-Djava.awt.h ...
- ElasticSearch概述及Linux下的单机ElasticSearch安装
原文链接:http://blog.csdn.net/w12345_ww/article/details/52182264 这两天在项目中要涉及到ElasticSearch的使用,就上网去搜索了一些这方 ...
- python nose测试框架全面介绍四
四.内部插件介绍 1.Attrib 标记,用于筛选用例 在很多时候,用例可以分不同的等级来运行,在nose中很增加了这个功能,使用attrib将用例进行划分 有两种方式: ef test_big_do ...