D
D
Domohod2021-12-26 15:52:45
Python
Domohod, 2021-12-26 15:52:45

How do I get data out of a Django model?

I have three pages where you can vote for 1 object.
On them you click on the "Choose" button and the result is displayed on the main page.

black.html

<form method="POST">
    {% csrf_token %}

        <button  class="button_b" name="choose" value="black">
            <h1 style="color: white">
                <strong>Choose</strong>
            </h1>
        </button>

        <h1 style="color: red">{{ count_black }}</h1> <!-- временный просмотр голосов-->
    </form>


models.py

#models.py
from django.db import models


class Choose(models.Model):
     count_black = models.PositiveIntegerField(default=0)
     count_white = models.PositiveIntegerField(default=0)
     count_purple = models.PositiveIntegerField(default=0)


My problem is that I don't know how to declare the model object in views.py.
I know that instead of "Choose" there should be a variable that takes objects from the Choose class and uses the QuerySet parameters, and also later use this variable in the context. However, I don't know exactly what parameters I need to set in order for the votes to be taken into account. Please help me.

views.py
def black(request):

    #пример переменной: name = Choose.objects.all()

    if request.method == 'POST':
        select_action = request.POST['choose']

        if select_action == 'black':
            Choose.count_black += 1

        # return redirect("home")
    Choose.save()

    return render(request, 'registration/black.html')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2021-12-26
@alfss

To have a choice out of the box, you need to make a model in which there is 1 field and enum.
https://docs.djangoproject.com/en/4.0/ref/models/f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question