基于python的投票系统源码求助

查看 62|回复 2
作者:夏殇i   
求一套python源码
基于Django的在线投票系统
CSDN源码
类似于以下这几种
功能不需要太丰富
https://blog.csdn.net/sheji508/article/details/129480402
https://www.bilibili.com/video/BV1qv4y1d7RX/?vd_source=141457f2239c5717a9ec155120c1d728
https://blog.csdn.net/sheji1213/article/details/128600674?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167871142116800211511362%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167871142116800211511362&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-9-128600674-null-null.142^v73^control,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=Django%E6%8A%95%E7%A5%A8%E7%B3%BB%E7%BB%9F&spm=1018.2226.3001.4187

源码, 投票系统

dode   

让GPT写一个,自己填充。
编写数据模型:在polls/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)
编写视图:在polls/views.py文件中编写视图函数,处理用户请求
[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,)))
编写模板:在polls/templates/polls目录中编写HTML模板文件,用于展示数据。
[HTML] 纯文本查看 复制代码{% if latest_question_list %}
   
    {% for question in latest_question_list %}
        
  • [url=]{{ question.question_text }}[/url]
        {% endfor %}
       

    {% else %}
        No polls are available.
    {% endif %}
  • dode   


    dode 发表于 2023-3-13 22:44
    让GPT写一个,自己填充。
    编写数据模型:在polls/models.py文件中定义数据模型,用于存储投票信息。
    [mw_ ...

    这些是GPT填写的,你可以自己去问细节再填充,没必要重复造轮子。
    您需要登录后才可以回帖 登录 | 立即注册

    返回顶部