gpt4 book ai didi

python - 如何停止在 django 上出现值错误?

转载 作者:行者123 更新时间:2023-12-04 09:40:56 25 4
gpt4 key购买 nike

我试图在运行我的代码时向我的 django 项目添加一个后续系统,我收到了 The QuerySet value for an exact lookup must be limited to one result using slicing.错误,是说views.py文件following_obj = Following.objects.get(user=user)这行代码出错了.

View .py

    def profile(request, username=None):
profile, created = Profile.objects.get_or_create(user=request.user)
if username:
post_owner = get_object_or_404(User, username=username)
user_posts = Profile.objects.filter(user_id=post_owner)
user = User.objects.filter(username=username)
is_following = Following.objects.filter(user=request.user, followed=user)
following_obj = Following.objects.get(user=user)
follower, following = following_obj.follower.count(), following_obj.followed.count()

else:
post_owner = request.user
user_posts = Profile.objects.filter(user=request.user)

args1 = {
'post_owner': post_owner,
'user_posts': user_posts,
'connection': is_following,
'follower': follower,
'following': following,
}
return render(request, 'profile.html', args1)

def follow(request, username):
main_user = request.user
to_follow = User.objects.get(username=username)
following = Following.objects.filter(user = main_user, followed = to_follow)
is_following = True if following else False
if is_following:
Following.unfollow(main_user, to_follow)
is_following = False
else:
Following.follow(main_user, to_follow)
is_following = True
resp = {
'following': is_following,
}

response = json.dumps(resp)
return HttpResponse(response, content_type="application/json")

模型.py
    class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(upload_to='profile_pics', null=True, blank=True, default='default.png')
bio = models.CharField(max_length=400, default=1, null=True)
follower = models.IntegerField(default=0)
following = models.IntegerField(default=0)

def __str__(self):
return f'{self.user.username} Profile'

class Following(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
followed = models.ManyToManyField(User, related_name="followed")
follower = models.ManyToManyField(User, related_name="follower")

@classmethod
def follow(cls, user, another_account):
obj, created = cls.objects.get(user = user)
obj.followed.add(another_account)
print("followed")

@classmethod
def unfollow(cls, user, another_account):
obj, create = cls.objects.get(user = user)
obj.followed.remove(another_account)
print("unfollowed")

def __str__(self):
return f'{self.user.username} Profile'

个人资料.html
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
{% if connection and not request.user == post_owner %}
<a type="button" class="btn btn-outline-primary" id="follow" role="button" href="{% url 'follow' post_owner.username %}">Unfollow</a>
{% elif not connection and not request.user == post_owner %}
<a type="button" class="btn btn-outline-primary" id="follow" role="button" href="{% url 'follow' post_owner.username %}">Follow</a>
{% endif %}
<p>Follower : {{follower}} Following : {{following}}</p>
<script type="text/javascript">
$("#follow").click(function(e){
e.preventDefault();
var href = this.href;
$.ajax({
url : href,
success : function(response){
if(response["following"]){
$("#follow").html("Unfollow");
}
else{
$("#follow").html("Follow");
}
}
})
})
</script>

最佳答案

线路:

user = User.objects.filter(username=username)

返回 User 的集合对象,您应该使用 .get(…) [Django-doc] 检索单个对象:
user = User.objects.get(username=username)

关于python - 如何停止在 django 上出现值错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62333116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com