猿问

DoesNotExist at /profiles/user-profile/

我正在尝试添加跟随切换,但在我看来,我在匹配查询集时遇到错误。用户配置文件存在于数据库中,但我仍然不知道为什么会发生这种情况。我希望你能找出我的错误并告诉我如何解决它。我正在学习 Django,如果你能帮助我,那意义重大。我将感谢你。如果需要更多代码来帮助我,我会分享。


class UserProfileFollowToggle(View):

    def post(self, request, *args, **kwargs):

        print(request.POST)

        user_to_toggle = request.POST.get('username')

        print(user_to_toggle)

        profile_ = UserProfile.objects.get(user__username__iexact=user_to_toggle)

        user = request.user

        if user in profile_.follower.all():

            profile_.follower.remove(user)

        else:

            profile_.follower.add(user)

        return redirect("/posts/list/") 

follow_toggle.html:


<form class='form' method='POST' action={% url 'profiles:toggle' %}>

{% csrf_token %}

<input type='hidden' name='username' value="{% if username %}{{ username }} {% else %}{{ testuser }}{% endif %}">


<button class='btn {% if is_following %}btn-default {% else %}btn-primary{% endif %}'>{% if is_following %}Unfollow {% else %}Follow{% endif %}</button>

</form>



有只小跳蛙
浏览 99回答 1
1回答

阿波罗的战车

它将在末尾呈现一个空格。因此,模板应如下所示:#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no space ↓<input type='hidden' name='username' value="{% if username %}{{ username }}{% else %}{{ testuser }}{% endif %}">
随时随地看视频慕课网APP

相关分类

Python
我要回答