Issue
I am trying to write a POST request for a game API and am passing some data from the GET request and some through this POST request. However, I keep getting the following error:
MultiValueDictKeyError 'gameround'
What am I doing wrong here?
def post(self, request, *args, **kwargs):
if not isinstance(request.user, CustomUser):
current_user_id = 1
else:
current_user_id = request.user.pk
gameround = request.GET['gameround']
random_resource = request.GET['resource']
created = datetime.now()
score = 0
origin = ''
name = request.POST['name']
language = request.POST['language']
user_input_tag = Tag.objects.create(name=name, language=language)
tag_serializer = TagSerializer(user_input_tag)
if Tagging.objects.all().filter(tag=user_input_tag).exists():
# if tagging like this exists, save tagging anyway and leave tag unchanged
score += 5
user_input_tagging = Tagging.objects.create(user_id=current_user_id,
gameround=gameround,
resource=random_resource,
tag=user_input_tag,
created=created,
score=score,
origin=origin)
tagging_serializer = TaggingSerializer(user_input_tagging)
return Response({'tag and ': tag_serializer.data}, {'tagging': tagging_serializer.data})
elif not Tagging.objects.all().filter(tag=user_input_tag).exists():
# save tagging otherwise and del tag?
user_input_tagging = Tagging.objects.create(user_id=current_user_id,
gameround=gameround,
resource=random_resource,
tag=user_input_tag,
created=created,
score=score,
origin=origin)
user_input_tagging.save()
tagging_serializer = TaggingSerializer(user_input_tagging)
return Response({'tagging only': tagging_serializer.data})
Solution
You don't have key gameround
in your GET
. You can get gameround data as
gameround = request.GET.get('gameround')
If default value is not given then it defaults to None
.
Or you can set default
value as
gameround = request.GET.get('gameround', '')
Answered By - user8193706
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.