为什么我收到内部服务器错误500

时间:2020-07-22 21:14:32

标签: javascript json django ajax django-views

我一直非常努力地进行了大量调查,试图在我的django项目中实现关注系统。完成代码后,每当我按“关注”按钮时,服务器都会返回此错误Failed to load resource: the server responded with a status of 500 (Internal Server Error),该怎么办才能停止出现此错误?我认为问题出在我代码的JavaScript上。

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    connection = models.CharField(max_length = 100, blank=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, create = cls.objects.get_or_create(user = user)
        obj.followed.add(another_account)
        print("followed")

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

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

views.py

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")

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

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

profile.html

<head>
    <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
</head>
<body>
{% if connection %}
    <a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Unfollow</a>
{% elif not connection %}
    <a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Follow</a>
{% endif %}
<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>
</body>

enter image description here

0 个答案:

没有答案