Django(投票系统项目)
1、创建一个Django项目:mysite
2、在mysite里创建一个static目录
3、在static里创建一个img目录,用来存放一些背景图片(图片随意)
4、完成一下基本的配置
在settings.py文件中进行数据库配置。
5、配置静态文件目录
6、数据迁移,因为我使用的是自带数据库,所以不需要执行迁移命令,只需要在数据库软件中新建一个连接,再执行sql文件就可以了
查看生成的数据表
7、路由配置
进行一个主路由的配置
8、创建应用----polls
在控制台执行python manage.py startapp index
9、注册polls应用,创建了一个应用就必须在settings.py文件中进行注册,不然项目会报错
10、在polls应用中新建一个templates目录,在templates目录中再创建一个polls目录,用来存放html文件。
11、在polls的models.py文件中创建Question和Choice的模型类
12、创建视图函数
在polls里的view.py文件中创建几个视图函数
这是源代码 from django.shortcuts import render,reverse,get_object_or_404from django.http import HttpResponse,HttpResponseRedirectfrom .models import Question,Choicedef index(request):latest_question_list = Question.objects.order_by('-pub_date')[:5]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)print(question)return render(request, 'polls/detail.html',{'question':question})def vote(request,question_id):question = get_object_or_404(Question, pk=question_id)try:selected_choice = question.choice_set.get(pk=request.POST['choice'])except (KeyError,Choice.DoesNotExist):return render(request,'polls/detail.html',{'question':question,'error_message':"You didn't select a choice."})else:selected_choice.votes += 1selected_choice.save()return HttpResponseRedirect(reverse('polls:results',args=(question.id,)))def results(request,question_id):question = get_object_or_404(Question, pk=question_id)return render(request, 'polls/results.html', {'question':question})
13、在主路由中导入这几个视图函数
14、做数据迁移,生成投票问题和选项表
在控制台一次执行这两次命令
查看生成的表
15、在polls下的urls.py路由文件中进行相应的配置,以连接相应的视图
16、新建三个html文件
17、进行每个html文件的配置
index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>index</title>{% load static %}<link rel="stylesheet" type="indexs" href="{% static 'static/index.css' %}" /> </head> <body style="background-image: url({% static 'img/3.jpg' %});background-size: 100% 200%;background-repeat: no-repeat;"> <center>{% if latest_question_list %}<ul><li></li><br><li></li><br><li></li><br><li></li><br></ul><ul>{% for question in latest_question_list %}<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>{% endfor %}</ul> {% else %}<p>No polls are available.</p> {% endif %} </center> </body> </html>
detail.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title>{% load static %}<link rel="stylesheet" type="indexs" href="{% static 'static/index.css' %}" /></head> <body style="background-image: url({% static 'img/3.jpg' %});background-size: 100% 150%;background-repeat: no-repeat;"> <center><ul><li></li><br><li></li><br><li></li><br><li></li><br></ul> <h2>{{ question.question_text }}</h2> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} <form action="{% url 'polls:vote' question.id %}" method="post">{% csrf_token %}{% for choice in question.choice_set.all %}<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"><lable for="choice{{ forloop.counter }}">{{ choice.choice_text }}</lable><br>{% endfor %}<input type="submit" value="提 交"> </form> </center> </body> </html>
results.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title>{% load static %}<link rel="stylesheet" type="indexs" href="{% static 'static/index.css' %}" /> </head> <body style="background-image: url({% static 'img/3.jpg' %});background-size: 100% 150%;background-repeat: no-repeat;"> <center><ul><li></li><br><li></li><br><li></li><br><li></li><br></ul><h2>{{ question.question_text }}</h2><ul>{% for choice in question.choice_set.all %}<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>{% endfor %}</ul><a href="{% url 'polls:detail' question.id %}">再投一次?</a> </center> </body> </html>
18、到这里,这个项目就已经配置完成了,接下来我们启动这个项目,看一下效果
Django(投票系统项目)相关推荐
- 基于微信公众号的答题投票系统——项目开发心得体会记录
项目背景 项目需求 后台管理功能 用户功能 页面展示 项目信息 开发语言 数据库 项目构想 获取幸运用户 用户答题 项目反思 项目进度的安排 团队合作沟通方面 项目的构建 技术 本地开发和上线的模式区 ...
- python投票系统项目ppt_python fastApi实战项目 - 爱投票管理系统(一)
一.闲来无事,在工作之余自己研究了一下python的异步框架 - fastapi,并写包括 1.部门管理 2.角色管理 3.用户管理 4.菜单管理 5.登录日志 6.操作日志 六个基础功能模块,演示链 ...
- 投票器JAVA的具体框架是_java毕业设计_springboot框架的投票系统
今天介绍一个java毕设题目, 题目内容为springboot框架的投票系统, 是一个采用b/s结构的javaweb项目, 采用java语言编写开发工具eclipse, 项目框架jsp+springb ...
- Django项目实战——用户投票系统(三)
Django项目实战--用户投票系统(三) 承接上文 官方文档链接附上: 编写你的第一个 Django 应用,第 3 部分 | Django 文档 | Django (djangoproject.co ...
- Django快速开发之投票系统
参考官网文档,创建投票系统. ================ Windows 7/10 Python 2.7.10 Django 1.8.2 ================ 1.创建项目(mys ...
- 学科投票系统-基于Python-Django实现的前后端分离项目
学科投票系统-基于Python-Django实现的前后端分离开发 作者:代昌松 项目详情代码请参考: vote_api:https://gitee.com/dcstempt_ping/vote_api ...
- python实训项目-Python开发基础-项目实训-在线投票系统.pptx
项目实训-在线投票系统本章任务/30完成"在线投票系统"添加投票候选人删除候选人为候选人投票按序号投票删除投票输出统计信息--本章目标/30理解程序的基本概念会使用顺序.选择.循环 ...
- python在线投票系统讲解_Python开发基础-项目实训-在线投票系统ppt课件
<Python开发基础-项目实训-在线投票系统ppt课件>由会员分享,可在线阅读,更多相关<Python开发基础-项目实训-在线投票系统ppt课件(27页珍藏版)>请在人人文库 ...
- javaweb简单小项目-投票系统
这次给大家带来一个课堂基础作业,简单的javaweb投票系统. 当然也设计了添加投票人的接口,只是list页面没有直接导航过去. 如页面显示,可以增加和减少相应的票数.同时id是利用数据库该字段作为主 ...
最新文章
- 一帖搞定U盘系统制作及安装苹果mac os引导U盘安装windows7
- Rust基础笔记:Getting input from the console
- python root_python在非root权限下的安装方法
- 安装Ubuntu 14.04后要做的5件事情
- mysql返回页面乱码java_解决Java程序使用MySQL时返回参数为乱码的示例教程
- java中static、final 和 static final之间的区别
- 信息检索 python_python-工程数据结构/信息检索和存储
- mysql 某列加全文索引_MySQL使用全文索引(fulltext index)---高性能
- Java实验7 四、Java异常类(2)创建自己的日期错误异常类
- 石头扫地机器人音量怎么调_石头的新扫地机器人 T6,内外都有升级
- 微信小程序云开发教程-后端接口分析和接口返回值的格式定义
- CUDA入门(六) 异步并行执行解析
- SCPPO(十二):SQL误操作如何恢复?
- 学习网络编程推荐安装的软件
- 美洽SDK通过广播结束消息提示
- PHP开发工具phpDesigner 7 (最新版,含注册机)
- 微信模板消息发送帮助类
- 英特尔显卡linux管理_Intel Linux 显卡驱动安装指南
- php网站源码木马查杀检测工具
- 数学模型之最小二乘法