Python Can';t在Django上添加/接受和拒绝/取消好友请求
能够成功发送好友请求,但响应请求是一个问题。当您按“接受”添加好友时,按钮将被删除,但好友不会被添加;或者当您按“取消”拒绝好友时,不会发生任何事情 尝试添加一个表单Python Can';t在Django上添加/接受和拒绝/取消好友请求,python,django,facebook-friends,Python,Django,Facebook Friends,能够成功发送好友请求,但响应请求是一个问题。当您按“接受”添加好友时,按钮将被删除,但好友不会被添加;或者当您按“取消”拒绝好友时,不会发生任何事情 尝试添加一个表单 class Add_Friend(forms.ModelForm): model = UserProfile def add_friend(request, user_profile): request.notification_set.get(type=Notification.FRIEND_R
class Add_Friend(forms.ModelForm):
model = UserProfile
def add_friend(request, user_profile):
request.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
request.friends.add(user_profile)
user_profile.friends.add(self)
request.friend_requests.remove(user_profile)
noti = Notification.objects.create(owner=user_profile, type=Notification.ACCEPTED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friends.count()
url(r'^friend request/send/(?P[\w\-]+)/$,send\u friend\u request),
url(r'^friend request/cancel/(?P[\w\-]+)/$,cancel\u friend\u request),
url(r'^friend request/accept/(?P[\w\-]+)/$,accept\u friend\u request),
{%if userprofile.friends.all%}
除掉朋友
{%elif userprofile.friend_requests.all%}
好友请求挂起。。。
取消
{%else%}
发送好友请求
{%endif%}
我希望用户能够接受/拒绝好友请求。不理解您的模板,有三个表单,但没有提交按钮。如果有你没有展示的地方?HTML中没有任何东西会触发对任何视图的调用。此外,您所说的“接受好友请求”/“拒绝好友请求”功能也不存在,因此此模板似乎与您的问题无关。最后,您的Django表单
Add\u friend
没有在视图中的任何位置使用,那么您为什么要显示它呢?
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(blank=True, max_length=128)
friends = models.ManyToManyField('self', blank=True, related_name='friends')
friend_requests = models.ManyToManyField('self', blank=True, related_name='friend_requests')
def send_friend_request(self, user_profile):
self.friend_requests.add(user_profile)
noti = Notification.objects.create(owner=self, type=Notification.FRIEND_REQUEST, sender=user_profile.user.username)
self.notification_set.add(noti)
return self.friend_requests.count()
def add_friend(self, user_profile):
self.friend_requests.remove(user_profile)
self.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
self.friends.add(user_profile)
user_profile.friends.add(self)
noti = Notification.objects.create(owner=user_profile, type=Notification.ACCEPTED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friends.count()
def cancel_friend_request(self, user_profile):
self.friend_requests.remove(user_profile)
self.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
noti = Notification.objects.create(owner=user_profile, type=Notification.DECLINED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friend_requests.count()
def __str__(self):
return self.get_first_name()
#Takes you to the userprofile page
def get_absolute_url(self):
return "/users/{}".format(self.id)
@method_decorator(login_required, name='dispatch')
class SendFriendRequestView(View):
def get(self, request, *args, **kwargs):
profile_id = request.GET.get('profile_id')
requester_id = request.GET.get('requester_id')
target = UserProfile.objects.get(id=profile_id)
requester = UserProfile.objects.get(id=requester_id)
target.send_friend_request(requester)
message = 'Friend request to {} sent!'.format(target.visible_name)
messages.info(request, message)
return redirect('profile', username=target.user.username)
@method_decorator(login_required, name='dispatch')
class CancelFriendRequestView(View):
def cancel_friend_request(request, id):
if request.user.is_authenticated():
user = get_object_or_404(User, id=id)
frequest, created = FriendRequest.objects.filter(
from_user=request.user,
to_user=user).first()
frequest.delete()
return HttpResponseRedirect('/users')
@method_decorator(login_required, name='dispatch')
class AddFriendView(View):
def get(self, request, *args, **kwargs):
try:
profile_id = request.GET.get('profile_id')
requester_id = request.GET.get('requester_id')
target = UserProfile.objects.get(id=profile_id)
requester = UserProfile.objects.get(id=requester_id)
target.add_friend(requester)
message = 'Added friend {}!'.format(target.visible_name)
messages.info(request, message)
return redirect('friends', username=target.user.username)
except Exception as e:
print('Error: {}'.format(e))
url(r'^friend-request/send/(?P<id>[\w\-]+)/$', send_friend_request),
url(r'^friend-request/cancel/(?P<id>[\w\-]+)/$', cancel_friend_request),
url(r'^friend-request/accept/(?P<id>[\w\-]+)/$', accept_friend_request),
{% if user.userprofile in userprofile.friends.all %}
<form>
<button id="remove_friend" data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Remove friend
</button>
</form>
{% elif user.userprofile in userprofile.friend_requests.all %}
Friend request pending...
<form>
<button id="cancel_friend_request " data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Cancel
</button>
</form>
{% else %}
<form>
<button id="send_friend_request" data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Send friend request
</button>
</form>
{% endif %}