【Web开发二】Django框架中部署一套投票网站
Django原理,URLS路由接收到客户端访问的请求------>view视图函数进行请求的处理-------->models模型(数据库)进行数据的处理--------->view视图函数进行数据的处理--------->template模板(HTML)进行数据的展示,反馈给客户端
一、初始化数据库,生成管理后台的用户。登录管理后台
python manage.py migrate #初始化数据库
python manage.py createsuperuser #创建超级用户
二、创建模型,模型,也就是关联到数据库的类函数,例如投票系统,那么就有投票项,投票项创建时间,投票内容选项,投票内容选项创建时间,票数。遵从数据库的准则,我们把投票项,和投票内容选项分开在两个表。
打开models.py
from django.db import models
# Create your models here.
class Question(models.Model): #创建这样类,继承于父类
question_text = models.CharField(max_length=200) #创建实例,类型为字符,最大长度200
publish_date = models.DateTimeField('publish date') #创建实例,类型为时间,声明为xxxx
def __str__(self): 使用str方法,返回问题内容
return self.question_text
class Choice(models.Model): #创建这样类,继承于父类
choice_text = models.CharField(max_length=100) #创建实例,类型为字符,最大长度100
publish_date = models.DateTimeField('publish date') #创建实例,类型为时间,声明为xxxx
vote = models.IntegerField(default=0) #创建实例,类型为数字,默认为0
question = models.ForeignKey(Question, on_delete=models.CASCADE) #创建实例,类型为(Question)外键,删除关联
def __str__(self): 使用str方法,返回问题选项内容
return self.choice_text
三、把应用添加到项目里面,把模型注册到后台管理。并且优化管理后台。
1、打开settings.py
2、打开admin.py
from django.contrib import admin
from .models import Question, Choice #导入模块
class QuestionAdmin(admin.ModelAdmin): #新建模型,集成于父类
list_display = ('question_text', 'publish_date') #优化后台管理器显示
list_filter = ('publish_date',) #使用时间作为过滤器
search_fields = ('question_text',) #使用内容作为搜索器
ordering = ('-publish_date', 'question_text') #使用降序时间排序,使用问题排序
date_hierarchy = 'publish_date' #使用时间作为时间轴
class ChoiceAdmin(admin.ModelAdmin):
list_display = ('question', 'choice_text', 'vote', 'publish_date')
list_filter = ('publish_date',)
search_fields = ('choice_text',)
ordering = ('-publish_date', 'vote')
date_hierarchy = 'publish_date'
raw_id_fields = ('question',) #优化选项,效果是显示question详情
admin.site.register(Question, QuestionAdmin) #注册到管理后台
admin.site.register(Choice, ChoiceAdmin)
3、效果如图
4、我们都说了,模型就是关联到数据库的。现在模型有了。那么就应该把模型。应用到数据库上,让数据库生成模型里面的字段。
python manage.py makemigrations #生成数据库语句
python manage.py migrate #执行语句
三、在web页面创建问题和相关选项。
生成问题
生成对应问题的选项
四、查看数据库,可以得到,polls_question和polls_choice这2个表格,有了刚刚我们输入的内容了。并且,Django自动为他们加入了id主键。并且choice表格里面有外键约束。
五、制作主页,走一遍Django的工作原理
根据Django的工作原理,我们第一步要修改的,应该是应用的urls文件
1、打开urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
这里已经写好去index的路由了。交友view里面的index函数处理。
2、打开views.py
from django.shortcuts import render, HttpResponse
from .models import Question, Choice #导入模块
# Create your views here.
def index(request): #创建index函数
question = Question.objects.all() #定义实例question,值为Question的对象查询集合
return render(request, 'polls/index.html', {'question': question}) #发送数据到polls下的index.html,参数名为question,值为question
3、创建模板目录,创建模板文件
mkdir -p polls/templates/polls
4、编写index.html文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>polls主页</title>
</head>
<body>
<ul>
{% for q in question %}
<li>
<a href="此处填写的是超链接跳转的页面,暂时留空">
{{ q.question_text }}
</a>
{{ q.publish_date }}
</li>
{% endfor%}
</ul>
</body>
</html>
效果如图
六、制作每个问题的详情页,实现访问主页,并且会进行跳转。
1、惯例修改urls文件。
打开urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'(?P<question_id>\d+)/$', views.detail, name='detail') ###规划url,我们用数字来做一个变量。然后根据数字来匹配polls_question的id,实现准确的定位到question_text。
]
2、修改views.py文件。新增detail视图函数,获取数据,然后发送给模板文件。
def detail(request, question_id): #question_id是从url这里获取的
question = Question.objects.get(id=question_id) #通过get的方法,获得对应的question实例
return render(request, 'polls/detail.html', {'question': question})
3、创建detail.html文件,然后根据实际需求,编写对应的视图界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>投票详情页</title>
</head>
<body>
<p>{{ question }}</p>
<form action="此处留空,作用是提交数据到vote的">
{% for c in question.choice_set.all %}
<input type="radio" name="c_id" value="{{c.id}}">{{ c.choice_text }} ##name和value处,后面会说明
{% endfor %}
<input type="submit" value="提交">
</form>
</body>
</html>
4、修改主页index.html,为超链接匹配detail.tml的模板
打开index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>polls主页</title>
</head>
<body>
<ul>
{% for q in question %}
<li>
<a href="{% url 'detail' question_id=q.id %}"> #调用url模块,匹配detail视图函数,作为参数的question。值为q.id
{{ q.question_text }}
</a>
{{ q.publish_date }}
</li>
{% endfor%}
</ul>
</body>
</html>
到此,我们实现了。超链接跳转的问题了。
七、制作投票结果的工作
1、urls.py文件新增记录
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'(?P<question_id>\d+)/result/$', views.result, name='result'), #新增
]
2、新增views.py文件中的函数
def result(request, question_id):
question = Question.objects.get(id=question_id)
return render(request, 'polls/result.html', {'question': question})
3、新增模板中的result.html文件,根据实际需求规划好数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>投票结果页</title>
</head>
<body>
<table border="1px">
<tr>
<td colspan="2">{{ question }}</td>
</tr>
{% for c in question.choice_set.all %}
<tr>
<td>
{{ c.choice_text }}
</td>
<td>
票数:{{ c.vote }}
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
效果如图
八、创建投票的程序,思路应该是这样的,在主页中,选择哪个问题,就相当于用question_id,打开对应的详情页,然后在详情页里面,选择哪个选项,选项的id会从表单中post到vote的程序。vote程序对数据进行+1,进行投票。然后投票结束后反馈结果页面给客户端。我们先做detail主页的代码,然后再完善投票程序。
1、
1、新增urls.py文件的代码
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'(?P<question_id>\d+)/result/$', views.result, name='result'),
url(r'(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
]
2、新增views.py文件记录。
def vote(request, question_id): #创建vote程序,参数question_id是从detail中获取
choice_id = request.POST.get('c_id') #利用request的方法,获取参数c_id
c = Choice.objects.get(id=choice_id) #获取到Choice的实例
c.vote += 1 #票数+1
c.save()
return redirect('result', question_id=question_id) #重定向到result页面。
3、修改detail.py文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>投票详情页</title>
</head>
<body>
<p>{{ question }}</p>
<form action="{% url 'vote' question_id=question.id %}" method="post"> #方式为post,然后提交数据到vote,参数为XXXXX
{% for c in question.choice_set.all %}
<input type="radio" name="c_id" value="{{ c.id }}">{{ c.choice_text }}
{% endfor %}
<input type="submit" value="提交">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
基本完成
效果如下
css样式,bootstrap样式,返回首页等等都可以添加。