dimanche 12 juillet 2020

Attribute Error: 'function' object has no attribute 'randint'

I'm having trouble with using the random module in my Django app. When I open up a path that should display a random number, I get an error that says: 'function' object has no attribute 'randint'.

I've looked this error up, and most people say that this error is because there is a stray random.py file floating around somewhere. Proof there's only one random.py file in my computer, and it is definitely the one that came with the python install. I'm 100% certain, because I uninstalled python 3.8.3 and reinstalled it twice. Here are my imports:

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django import forms


from . import util
import os, re, random

Here's the code in my views.py file that is calling random.randomint.

def entry(request, title):
    entry = util.get_entry(title)
    randomNum = random.randint(0, 10)
    return render(
            request,
            "encyclopedia/entry.html",
            {"entry": entry, "randomNum": randomNum},
        )

Then I have an HTML file that displays . When that template gets loaded, I get this error screen. I'm banging my head against the wall trying to figure this out, so any help is greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire