samedi 17 octobre 2020

Django shows valid object as NoneType

views.py

def profile(request, username):    
    user = User.objects.filter(username=str(username)).first()
    posts = Post.objects.filter(user=user).count()
    followers = User.objects.filter(following=user).count()
    following = user.following.all().count()
    return render(request, "network/profile.html", {
        "user": user,
        "posts": posts,
        "followers": followers,
        "following": following
    })

urls.py

path("<str:username>/", views.profile, name="profile"),

models.py

class User(AbstractUser):
    following = models.ManyToManyField("self")

django shows this error-

File "C:\Users\Mufaddal\Desktop\cs50w\project4Network\network\views.py", line 98, in profile following = user.following.all().count() AttributeError: 'NoneType' object has no attribute 'following'

The website shows the username, posts, following, followers correctly which means the query is returning a valid user. Then why does django show this error?




Aucun commentaire:

Enregistrer un commentaire