我正在使用Django 2.2和PostgreSql。我正在尝试创建一个想要跟踪邻居用户的简单应用。 “关注”按钮将增加关注的数量,“关注”按钮将减少关注的数量。但是,“关注”按钮不起作用。我该如何解决这个问题?
following / models.py
class Following(models.Model):
follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='fallower', null=True)
followed = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='fallowing')
following / views.py
def user_follow_unfollow(request):
response = sub_user_follow_unfollow(request)
data = response.get('data')
followed = response.get('followed')
numbers_followed_and_follower= Following.user_followed_and_follower(followed)
context = {'user': followed, 'followers': numbers_followed_and_follower['followers'],
'followeds': numbers_followed_and_follower['followeds']}
html = render_to_string('following/following_partion.html', context=context, request=request)
data.update({'html': html})
return JsonResponse(data=data)
def sub_user_follow_unfollow(request):
if not request.is_ajax():
return HttpResponseBadRequest()
data = {'follow status': True, 'html': '', 'is_valid': True, 'msg': '<b>Unfollow</b>'}
follower_username = request.GET.get('follower_username', None)
followed_username = request.GET.get('followed_username', None)
follower = get_object_or_404(User, username=follower_username)
followed = get_object_or_404(User, username=followed_username)
does_follow_the_user= Following.user_does_follow_the_user(follower=follower, followed=followed)
if not does_follow_the_user:
Following.user_follow(follower=follower, followed=followed)
else:
Following.user_unfollow(followed=followed, follower=follower)
data.update({'msg': '<b>Follow</b>', 'follow_status': False})
return {'data': data, 'followed': followed}
templates.following_partion.html
{% if request.neighbor_detail != user %}
<div>
<button followed='{{ neighbor_detail.username }}' followed='{{ request.neighbor_detail.username }}'
url="{% url 'following:user_follow_and_unfollow' %}" id="follow_unfollow_button"
class="btn btn-lg btn-success">
{% if does_follow_the_user%}
<b>Unfollow</b>
{% else %}
<b>Follow</b>
{% endif %}
</button>
</div>
{% endif %}
<div class="followers col-lg-offset-3 col-md-3 col-md-offset-3 col-lg-3 text-center">
<span><b>followers</b></span>
<button url="{% url 'following:fallowed-or-fallowers-list' 'fallowers' %}" follow_type="followers"
username="{{ neighbor_detail.username }}" class="follow_button btn-block btn btn-primary">
{{ followers}}
</button>
<div class="followeds col-lg-3 col-md-3 text-center">
<span><b>Followeds</b></span>
<button url="{% url 'following:followed-or-followers-list' 'followed' %}" follow_type="followed"
username="{{ neighbor_detail.username }}" class="follow_button btn-block btn btn-success">
{{ followeds}}
</button>
我的脚本
<script>
$("#follow_unfollow_button").click(function () {
var $this = $(this);
var $url = $this.attr('url');
var $takip_eden = $this.attr('follower');
var $takip_edilen = $this.attr('followed');
var data = {follower_username: $follower, followed_username: $followed};
$.ajax({
url: $url,
type: 'GET',
dataType: 'json',
data: data,
success: function (data) {
if (data.is_valid) {
$this.html(data.msg);
$("#user_following").html(data.html)
}
}
})
});
</script>
答案 0 :(得分:0)
我们缺少帮助,因为丢失的件太多了。但是,调试起来应该不太困难,这是您应该检查的一些内容:
request.GET
,data
,numbers_followed_and_follower
的值。如果您不知道这些,就不可能说出问题所在。您需要分步分析各种变量以了解问题所在。
建议
使用ManyToManyField
注册关注者。它们只是两个用户之间的关系:
follows = models.ManyToManyField("self", symmetrical=False, related_name="followers")
通过指定symmetrical=False
,您告诉Django区分关系的两个方向。为了在视图中创建或删除关系以及查看关系,您可以:
user.follows.add(other_user) # user follows other_user
user.follows.remove(other_user) # user unfollows other_user
user.followers.all() # people following
user.followers.count() # number of followers