mardi 2 novembre 2021

How to generate a random number in django? [closed]

I want to generate a random number to be added to a counter. But I am receiving an error and cant figure out why. Appreciate your help.

from django.shortcuts import render, redirect, HttpResponse
from random import random, randint

# Create your views here.
def index(request):
    if 'counter' not in request.session:
        request.session['counter'] = 0
    return render(request, 'index.html')

def process_money(request):
    print(request.POST['custId'])
    if request.method == 'POST':
        if request.session['custId'] == '1':
            request.session['counter'] += random.random() # Add a random number to the counter
        if request.session['custId'] == '2':
            request.session['counter'] += 10
        request.session['custId'] = request.POST['custId']    


    return render(request, 'index.html')

This is the error I get in the console: AttributeError: 'builtin_function_or_method' object has no attribute 'random'




Aucun commentaire:

Enregistrer un commentaire