这是我的看法。
def profile(request, username):
if User.objects.filter(username=username).exists():
u = User.objects.filter(username=username)[0]
if not Followers.objects.filter(user=username, follower=request.user.username).exists():
following = "Follow"
cls = "btn-p"
else:
following = "Following"
cls = "btn-t"
if u.profilepic == "":
u.profilepic = "static/assets/img/default.png"
followers_p = 0
following_p = 0
posts = 0
name = u.name
bio = u.bio
posts = Photo.objects.filter(owner=username)
posts = len(posts)
followers_p = len(Followers.objects.filter(user=username))
following_p = len(Followers.objects.filter(follower=username))
context = {
'ProfilePic': u.profilepic, "whosprofile": username, "logged_in_as": request.user.username, "following": following, "cls":cls, "posts":posts, "followers_p":followers_p, "following_p": following_p,"name":name, "bio":bio
}
if request.user.is_authenticated:
return render(request, 'logged-in-profile.html', context)
return render(request, 'profile.html', context)
答案 0 :(得分:1)
如果用户名不存在,则不会返回任何内容,因此它返回None。
def profile(request, username):
if User.objects.filter(username=username).exists():
......................
else:
return HttpResponse('No user found')
#return 'something'