简体   繁体   中英

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 {{ randomNum }} . 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!

I figured it out.,,. It's not that I had a file called random,py. but I did have a view path called path("random", views.random, name="random"). I deleted that and now randomint works!

Edit: Actually it wasn't the URL path, I also had a view in views.py called random. That was the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM