我想让投票按钮具有适当的功能,但是我无法做到这一点

时间:2019-08-25 13:33:16

标签: django python-3.7

我希望投票按钮能够正常工作,当用户单击该按钮时,它应该会自己增加投票数。

views.py

    from django.shortcuts import render, get_object_or_404
    from django.http import HttpResponse
    from . models import Question, Choice
    from django.urls import reverse

    # Create your views here.

    def base(request):
        return render(request, 'base.html')

    def home(request):
        return render(request, 'home.html')

    def quest(request):
        question_list=Question.objects.all()
        context={'question_list': question_list}
        return render(request, 'quest.html', context)

urls.py

    from django.urls import path
    from . import views

    urlpatterns = [
        path('', views.base, name='base'),
        path('home', views.home, name='home'),
        path('quest', views.quest, name='quest'),
    ]

base.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://kit.fontawesome.com/a076d05399.js"></script>
        <title>Voting World</title>
      </head>
      <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="{% url 'home' %}">Home</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
          <div class="navbar-nav ml-auto">
            <a class="nav-item nav-link active" href="#">About us</a>
            <a class="nav-item nav-link active" href="#"><i class="fas fa-user"></i> Login</a>
            <a class="nav-item nav-link active" href="#"><i class="fas fa-sign-in-alt"></i> Signup</a>
          </div>
        </div>
        </nav>

        <div class="container">


          {% block body %}



          {% endblock %}

        </div>


      </body>
    </html>

quest.html

    {% extends 'base.html' %}


    {% block body %}

    {% if question_list %}
        <ul style="list-style: none;">
            {% for question in question_list %}
                <div class="row mt-3 text-danger">
                    <li><h3>{{ question.title }}</h3><p style="color: #072e6e">{{ question.pub_date }}</p></li>
                </div>
                <div class="row mt-3">
                    <div class="col-md-5">
                        <li><img src="{{ question.image1.url }}"></li>
                    </div>
                    <div class="col-md-2">
                        <h1 style="font-size: 180px; margin-left: -95px;">v/s</h1>
                    </div>
                    <div class="col-md-5">
                        <li><img src="{{ question.image2.url }}"></li>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-5">
                        <a href="#"><button class="btn btn-success">Vote</button></a>
                    </div>
                    <div class="col-md-5">
                        <a href="#"><button class="btn btn-success">Vote</button></a>
                    </div>
                </div>
            {% endfor %}
        </ul>
    {% else %}
        <p>No Polls Available!!..</p>
    {% endif %}
    {% endblock %}

models.py

    from django.db import models

    # Create your models here.

    class Question(models.Model):
        title = models.CharField(max_length=100)
        image1 = models.ImageField(upload_to='pics')
        image2 = models.ImageField(upload_to='pics')
        pub_date = models.DateTimeField()

        def __str__(self):
            return self.title

    class Choice(models.Model):
        question = models.ForeignKey(Question, on_delete=models.CASCADE)
        vote1 = models.IntegerField(default=0)
        vote2 = models.IntegerField(default=0)

当用户单击投票时,我期望输出,它将使投票的值增加1,即,+ 1,但我不知道该怎么做。

0 个答案:

没有答案