Python-Django网上投票系统代码(需要连接数据库,实现增删改查等功能)
时间: 2024-03-16 19:42:59 浏览: 26
以下是一个简单的Python-Django网上投票系统代码,实现了连接数据库、增删改查等功能:
1. 创建Django项目和应用
首先,使用命令行创建一个新的Django项目:
```
django-admin startproject voting_system
```
然后,在项目目录下创建一个新的应用:
```
cd voting_system
python manage.py startapp polls
```
2. 定义模型
在应用的models.py文件中定义数据模型:
```python
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
```
这里定义了两个模型,Question和Choice。Question模型包含一个问题文本和发布日期,Choice模型包含一个选项文本、所属问题和投票数。
3. 数据库迁移
运行以下命令,创建表格:
```
python manage.py makemigrations
python manage.py migrate
```
4. 创建视图
在应用的views.py文件中定义视图函数,用于处理HTTP请求并返回响应:
```python
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .models import Question, Choice
def 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)
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.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 += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
```
这里定义了四个视图函数:index、detail、results和vote。index函数返回最新的5个问题列表,detail函数返回指定问题的详细信息,results函数返回指定问题的投票结果,vote函数处理投票请求并返回结果。
5. 创建模板
在应用的templates目录下创建HTML模板,用于渲染视图函数返回的数据。
6. 配置URL
在应用的urls.py文件中配置URL模式,将不同的URL请求映射到对应的视图函数。
```python
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
```
这里定义了四个URL模式,将空路径、问题详情、投票结果和投票请求映射到对应的视图函数。
7. 运行测试服务器
最后,运行以下命令启动测试服务器:
```
python manage.py runserver
```
然后在浏览器中访问http://localhost:8000/polls/即可查看投票系统的首页。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)