Home>

I am doing a vote in Python Django, the main page should display the result of all users' votes, but I only display the results of the current user. How to fix it?

views.py:

def home (request):
    value, create= Choose.objects.get_or_create (voter= request.user)
    context= {
        "value": value,
    }
    return render (request, 'registration /home.html', context)
def black (request):
    if request.user.is_authenticated:
        value, created= Choose.objects.get_or_create (voter= request.user)
        if request.method== 'POST':
            select_action= request.POST ['choose']
            if select_action== 'black':
                value.count_black += 1
                value.save ()
            else:
                return render (request, 'registration /black.html', {"value": value})
            return redirect ("home")

models.py:

class Choose (models.Model):
    count_black= models.PositiveIntegerField (default= 0, verbose_name= "black")
    count_white= models.PositiveIntegerField (default= 0, verbose_name= "white")
    count_purple= models.PositiveIntegerField (default= 0, verbose_name= "purple")
    voter= models.ForeignKey (User, null= True, blank= True, verbose_name= 'User',
    on_delete= models.PROTECT)

html:

<
h1 style= "color: white" >
{{value.count_black}} <
/h1 >

So, in my admin panel, the results of various users are saved, but only the voices of the current one (the one I signed in) are displayed. How do I display the entire result?

  • Answer # 1

    Fix:Choose.objects.get_or_create (voter= request.user)

    On:Choose.objects.all ()

    I've tried this already, but it doesn't output anything at all. Maybe there are other options?

    tammalako2021-12-31 08:45:13
  • Answer # 2

    Fix:Choose.objects.get_or_create (voter= request.user)

    On:Choose.objects.all ()

    I've tried this already, but it doesn't output anything at all. Maybe there are other options?

    tammalako2021-12-31 08:45:13