英文:
MultiValueDictKeyError on a Django form
问题
以下是代码中的错误信息和相关部分的中文翻译:
MultiValueDictKeyError at /unfollow
多值字典关键错误
'userfollow'
'userfollow'
Request Method: POST
请求方法:POST
Exception Type: MultiValueDictKeyError
异常类型:MultiValueDictKeyError
Exception Value: 'userfollow'
异常值:'userfollow'
我尝试使用request.POST.get()并获得了另一个错误。
在views.py文件中:
def follow(request):
userfollow = request.POST['userfollow']
currentUser = User.objects.get(pk=request.user.id)
userfollowData = User.objects.get(username=userfollow)
f = Follow(user=currentUser, user_follower=userfollowData)
f.save()
user_id = userfollowData.id
return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))
def unfollow(request):
userfollow = request.POST['userfollow']
currentUser = User.objects.get(pk=request.user.id)
userfollowData = User.objects.get(username=userfollow)
f = Follow.objects.get(user=currentUser, user_follower=userfollowData)
f.delete()
user_id = userfollowData.id
return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))
在profile.html文件中,你的HTML模板已经包括了一些中文字符,但没有明确的错误信息。
希望这可以帮助你找到代码中的问题。
英文:
I cant find the error in here when clicking the follow or unfollow button this message keeps coming.
MultiValueDictKeyError at /unfollow
'userfollow'
Request Method: POST <br>
Exception Type: MultiValueDictKeyError <br>
Exception Value:'userfollow'
I tried request.POST.get() and got another error.
views.py
def follow(request):
userfollow = request.POST['userfollow']
currentUser = User.objects.get(pk=request.user.id)
userfollowData = User.objects.get(username=userfollow)
f = Follow(user=currentUser, user_follower=userfollowData)
f.save()
user_id = userfollowData.id
return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))
def unfollow(request):
userfollow = request.POST['userfollow']
currentUser = User.objects.get(pk=request.user.id)
userfollowData = User.objects.get(username=userfollow)
f = Follow.objects.get(user=currentUser, user_follower=userfollowData)
f.delete()
user_id = userfollowData.id
return HttpResponseRedirect(reverse(profile, kwargs={'user_id': user_id}))
profile.html
{% extends "network/layout.html" %}
{% block body %}
<h1>{{ profile.username }}</h1>
<div class="container">
<div class="row d-flex justify-content-center">
<h3 class="col-4">Followers: {{ followers.count}} </h3>
<h3 class="col-4">Following: {{ following.count}} </h3>
{% if user.is_authenticated %}
{% if user != user_profile %}
{% if isFollowing %}
<form action="{% url 'unfollow' %}" method="post">
{% csrf_token %}
<input type="hidden" name"userfollow" value="{{ user_profile }}" />
<input type="submit" value="Unfollow" />
</form>
{% else %}
<form action="{% url 'follow' %}" method="post">
{% csrf_token %}
<input type="hidden" name"userfollow" value="{{ user_profile }}" />
<input type="submit" value="Follow" />
</form>
{% endif %}
{% endif %}
{% endif %}
</div>
</div>
{% endblock %}
Help would be appreciated!
答案1
得分: 0
更正name"userfollow"
为name="userfollow"
,这样才能正确向后端发送参数。
英文:
There is a typo in
<input type="hidden" name"userfollow" value="{{ user_profile }}" />
Change name"userfollow"
to name="userfollow"
, this is not sending the parameter properly to the backend
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论