django sets up a voting website

Keywords: PHP Django network

Yesterday I finally finished the voting page. How to say, I didn't think this book was very helpful to me either. Then I looked at django's documentation and found that it was the same project.

But let's start and end well by pasting the Chinese version of the document https://docs.djangoproject.com/zh-hans/2.1/

  

This time it's time for the form. Our voting system has successfully uploaded the questionnaire question, but there are no options. Let's go to admin to add the model, add admin.site.register(Choice) to admin, and don't forget to add Choice to the import model.

After restarting the server, go back to Background Management and find that you can add voting options, here I have a problem, before you created the model, that is, under the model file, when you constructed the model, you wrote an extra choice, which caused an error on this side. If you pressed my code before, you would come out hereIf there is no choice in a Choice, just delete it from the constructor in models. It should be like this

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)

    votes = models.IntegerField(default = 0)

    def __str__(self):
        return self.choice_text

For network reasons, this article uses no pictures at all.

Then go modify the detail file and create a form for submitting data

<h1>{{ question.question_text }}</h1>

{% 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}}"/>
        <label for="choice{{ forloop.counter}}" >{{choice.choice_text}}</label>
        <br />
        {% endfor %}
    <input type="submit" value="Submit" />
</form>

And then change vote and results under views

from django.http import HttpResponseRedirect,HttpResponse

from django.urls import reverse

from .models import Question,Choice

def vote(request,question_id):
    print("hello")
    question = get_object_or_404(Question,pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk = request.POST['choice'])
    except(KeyError,Choice.DoseNotExist):
        return render(request,'polls/detail.html',{
            'question':question,
            'error_message':"No options have been selected",
        })
    else:
        selected_choice.votes += 1
        selected_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})

This is to process the votes and create results.html under polls, adding the following code

<h1>{{question.question_text}}</h1>
<ul>
    {% for choice in question.choice_set.all %}
    <li>{{choice.choice_text}}--Counting{{choice.votes}}{{choice.votes|pluralize}}second</li>
    {% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Revoting</a>

Tip: Pay attention to the spacing between%and {} when writing these codes in your own handwriting, it will make a mistake.

Then, after completing the above steps, you will be able to vote

Posted by Luvac Zantor on Sun, 28 Jul 2019 18:44:10 -0700