问题描述
我一直在尝试添加朋友系统,该用户可以在其中添加和删除朋友(其他用户),在完成代码后,我发现一个错误,当登录的用户尝试从其他用户的个人资料中添加朋友时, 添加好友按钮重定向到已登录的用户个人资料,因此无法添加新好友,它只能将自己添加为好友。我个人认为该错误是在views.py个人资料视图上。
I have been trying to add friend system in which the user can add and remove friends (other users), after finishing with the code, I found an error that when the logedin user tries to add a friend from other user's profile, the add friend button redirects to the logedin user profile making it imposible to add a new friend, it can just add himself as a friend. I personally think the error is on the views.py profile view.
views.py(个人资料显示用户的个人资料,change_friend是添加和删除好友的个人资料)
views.py (profile shows user's profile and change_friend is the one that adds and removes frinds)
def profile(request, username=None):
friend = Friend.objects.filter(current_user=request.user).first()
friends = []
if friend:
friends = friend.users.all()
if username:
post_owner = get_object_or_404(User, username=username)
user_posts=Post.objects.filter(user_id=post_owner)
else:
post_owner = request.user
user_posts=Post.objects.filter(user=request.user)
args1 = {
'post_owner': post_owner,
'user_posts': user_posts,
'friends': friends,
}
return render(request, 'profile.html', args1)
def change_friends(request, operation, pk):
friend = User.objects.get(pk=pk)
if operation == 'add':
Friend.make_friend(request.user, friend)
elif operation == 'remove':
Friend.lose_friend(request.user, friend)
return redirect('profile')
models.py
models.py
class Friend(models.Model):
users = models.ManyToManyField(User, default='users', blank=True, related_name='users')
current_user = models.ForeignKey(User, related_name='owner', on_delete=models.CASCADE, null=True)
@classmethod
def make_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.add(new_friend)
@classmethod
def lose_friend(cls, current_user, new_friend):
friend, created = cls.objects.get_or_create(
current_user=current_user
)
friend.users.remove(new_friend)
profile.html
profile.html
<div class="media">
<div class="media-body">
<h2 class="account-heading">{{ post_owner.username }}</h2>
<p class="text-secondary">{{ post_owner.email }}</p>
{% if not user in friends %}
<a href="{% url 'change_friends' operation='add' pk=user.pk %}">
<button type="button">add Friend</button>
</a>
{% endif %}
</div>
</div>
<div>
<h2>Friends</h2>
{% for friend in friends %}
<p>{{ friend.username }}</p>
<a href="{% url 'change_friends' operation='remove' pk=friend.pk %}">
<button type="button">Remove Friend</button>
</a>
{% endfor %}
</div>
urls.py
urlpatterns = [
path('profile/<str:username>/', views.profile, name='profile_pk'),
url(r'^connect/(?P<operation>.+)/(?P<pk>\d+)/$', views.change_friends, name='change_friends'),
]
推荐答案
问题是您正在传递 request.user
对象通过 user
对象进入 change_friends
视图。在模板中使用时,默认情况下 user == request.user
。
The problem is that you're passing the request.user
object to the change_friends
view, through the user
object. By default user == request.user
when used in a template.
只需更改您所在的行profile.html,其中:
Just change that line that you have in profile.html with:
<a href="{% url 'change_friends' operation='add' pk=post_owner.pk %}">
<button type="button">add Friend</button>
</a>
现在,我注意到您正在将用户重定向到配置文件
视图,一旦他们添加了新朋友,那不是您想要的。发生这种情况是因为,当您在 change_friends
视图中调用 redirect()
函数时,没有传递任何参数转到个人资料
视图。您定义的用户名默认应为无
,然后说如果不是用户名
,则 post_owner
应该是 request.user
Now, I note that you're redirecting the user to the profile
view once they add a new friend, and that's not what you want. That's happening because when you're calling the redirect()
function in your change_friends
view, you are not passing any parameter to the profile
view. You defined the username should be None
by default and then you're saying that if not username
then post_owner
should be request.user
如何更改此设置?好吧,在调用 redirect
作为关键字参数时,只需传递所需的用户名
即可。如下所示:
How to change this? Well, just pass the desired username
when calling redirect
as a keyword argument. Something as follows:
return redirect('profile', username=friend.username)
这篇关于在Django上添加好友系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!