D
D
Domohod2021-12-27 09:39:03
Python
Domohod, 2021-12-27 09:39:03

Why does the click disappear when going to the page?

Using Python, django
I have a button on my page that counts the number of clicks on it. When I refresh the page, this number is saved, but if I go to another page, the value is reset to zero.
views.py:

def black(request):
    value = Choose()

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

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

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

    context = {"value": value}

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


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)

form(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">{{ value.count_black }}</h1> {# строка, выводящая значение #}
    </form>


And yet, when you click on the button, the value changes + by one once, and then remains the same. Why? Help me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question