• <tfoot id='6W6p0'></tfoot>

      <small id='6W6p0'></small><noframes id='6W6p0'>

      1. <i id='6W6p0'><tr id='6W6p0'><dt id='6W6p0'><q id='6W6p0'><span id='6W6p0'><b id='6W6p0'><form id='6W6p0'><ins id='6W6p0'></ins><ul id='6W6p0'></ul><sub id='6W6p0'></sub></form><legend id='6W6p0'></legend><bdo id='6W6p0'><pre id='6W6p0'><center id='6W6p0'></center></pre></bdo></b><th id='6W6p0'></th></span></q></dt></tr></i><div id='6W6p0'><tfoot id='6W6p0'></tfoot><dl id='6W6p0'><fieldset id='6W6p0'></fieldset></dl></div>

      2. <legend id='6W6p0'><style id='6W6p0'><dir id='6W6p0'><q id='6W6p0'></q></dir></style></legend>

        • <bdo id='6W6p0'></bdo><ul id='6W6p0'></ul>

        使用 POST 时 Django 中的 MultiValueDictKeyError

        时间:2023-09-28

        <tfoot id='6el98'></tfoot><legend id='6el98'><style id='6el98'><dir id='6el98'><q id='6el98'></q></dir></style></legend>
          • <bdo id='6el98'></bdo><ul id='6el98'></ul>
            <i id='6el98'><tr id='6el98'><dt id='6el98'><q id='6el98'><span id='6el98'><b id='6el98'><form id='6el98'><ins id='6el98'></ins><ul id='6el98'></ul><sub id='6el98'></sub></form><legend id='6el98'></legend><bdo id='6el98'><pre id='6el98'><center id='6el98'></center></pre></bdo></b><th id='6el98'></th></span></q></dt></tr></i><div id='6el98'><tfoot id='6el98'></tfoot><dl id='6el98'><fieldset id='6el98'></fieldset></dl></div>

                <tbody id='6el98'></tbody>

                <small id='6el98'></small><noframes id='6el98'>

                  本文介绍了使用 POST 时 Django 中的 MultiValueDictKeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Django REST 框架的新手,被要求编写我们项目的令牌认证部分.需要注意的一件事是,由于将来我将不使用默认管理站点,因此我编写了登录、注销、注册功能,并通过 POSTMAN 测试功能.我现在要做的是让新用户注册,登录和注销.当用户登录时,我会发给他/她一个令牌.一切都以最简单的方式执行.

                  I am new to Django rest framework and was asked to write the token authentication part of our project. One thing to note is, as I would use not use the default admin site in future, I write login, logout, signup functions, and test the functionality by POSTMAN. What I want to do now is to let new user signup, login and logout. When a user log in, I issue him/her a token. Everything just perform in the easiest way.

                  但我仍然无法解决.我搜索了所有相关问题,但仍然无法解决我的问题.如果有人知道该怎么做,请帮助我!以下是详细内容.

                  But I still can't work it out. I searched all the related questions but still cannot solve my problem. If someone know how to do, please help me! Following is the details.

                  当我使用 GET 时,一切正常.但是当我使用 POST 时,我得到 MultiValueDictKeyError.我不知道为什么.

                  When I am using GET, everything works fine. But when I am using POST, I get MultiValueDictKeyError. I don't know why.

                  View.py

                      from rest_framework.response import Response
                      from django.contrib.auth import authenticate
                      from rest_framework.authtoken.models import Token
                      from rest_framework import status
                      from django.contrib.auth.models import User
                      from rest_framework.authentication import TokenAuthentication
                      from rest_framework.permissions import IsAuthenticated
                      from django.contrib.auth.signals import user_logged_in, user_logged_out
                      from rest_framework.decorators import api_view, authentication_classes, permission_classes
                      from django.views.decorators.csrf import csrf_exempt, requires_csrf_token
                      @csrf_exempt
                      @requires_csrf_token
                      @api_view(['POST'])
                      def create_user_view(request):
                          if request.method == 'POST':
                              username = request.POST['username']
                              email = request.POST['email']
                              password = request.POST['password']
                              user = User.objects.create_user(username=username, email=email,     password=password)
                              user.save()
                              return Response({'detail': "Create user"})
                  
                  
                      @csrf_exempt
                      def login_view(request):
                           if request.method == 'POST':
                              username = request.POST['username']
                              password = request.POST['password']
                              user = authenticate(username=username, password=password)
                           if user is not None:
                              user_logged_in.send(sender=user.__class__, request=request, user=user)
                              token = Token.objects.get_or_create(user=user)
                              return Response({
                                             'detail': 'POST answer', 'token': token[0].key,
                                             })
                          else:
                              return Response({'detail': "The username or password were incorrect.",
                             status: status.HTTP_404_NOT_FOUND})
                  
                      @csrf_exempt
                      def logout_view(request):
                          if request.method == 'POST':
                              user = getattr(request, 'user', None)
                              if hasattr(user, 'is_authenticated') and not user.is_authenticated():
                                  user = None
                                  user_logged_out.send(sender=user.__class__, request=request, user=user)
                              if hasattr(request, 'user'):
                                   from django.contrib.auth.models import AnonymousUser
                  
                                   request.user = AnonymousUser()
                             return Response({'detail': "You have logged out successfully."})
                  

                  Test_app/Urls.py

                      from django.conf.urls import patterns, url
                      from rest_framework.urlpatterns import format_suffix_patterns
                      from test_app import views
                      urlpatterns = patterns('test_app.views',
                                             url(r'^signup', views.create_user_view),
                                             url(r'^login', views.login_view),
                                             url(r'^logout', views.logout_view),
                                             url(r'^auth', views.AuthView),
                                             )
                      urlpatterns = format_suffix_patterns(urlpatterns)
                  

                  Models.py(是的,我只在文件中放了两行)

                      from django.contrib.auth.models import User
                      from django.db import models
                  

                  我还按照教程中的说明修改了 settings.py.

                  I also modified settings.py as tutorial said.

                  现在的问题是:

                  Request Method: POST
                  Request URL:    http://127.0.0.1:8000/signup?username=haha&email=haha@gmail.com&password=okok
                  Django Version: 1.8.2
                  Exception Type: MultiValueDictKeyError
                  Exception Value:    
                  "'username'"
                  Exception Location: /Users/wyq/PycharmProjects/env/lib/python2.7/site-packages/django/utils/datastructures.py in __getitem__, line 322
                  

                  有人可以帮忙吗?非常感谢!

                  Can anyone help? Thank you very much!

                  推荐答案

                  MultiValueDictKeyError 发生在 QueryDict 当您尝试访问的键不在 QueryDict 中时.您的请求方法说 POST 但 url 方案建议您传递的参数将进入 request.GET 字典.

                  MultiValueDictKeyError occurs in a QueryDict when the key you are trying to access is not present in the QueryDict. Your request method says POST but the url scheme suggests that the parameters you are passing will go the request.GET dict.

                  您需要通过表单或其他方式提交参数,以便在 request.POST QueryDict

                  You need to submit the parameters via a form or something for them to be accessible in the request.POST QueryDict

                  这篇关于使用 POST 时 Django 中的 MultiValueDictKeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何修复&lt;响应 500&gt;python请求中的错误? 下一篇:如何使用 Postman 表单数据在 Django REST Framework 中发布嵌套数组?

                  相关文章

                  <small id='JTLfG'></small><noframes id='JTLfG'>

                • <i id='JTLfG'><tr id='JTLfG'><dt id='JTLfG'><q id='JTLfG'><span id='JTLfG'><b id='JTLfG'><form id='JTLfG'><ins id='JTLfG'></ins><ul id='JTLfG'></ul><sub id='JTLfG'></sub></form><legend id='JTLfG'></legend><bdo id='JTLfG'><pre id='JTLfG'><center id='JTLfG'></center></pre></bdo></b><th id='JTLfG'></th></span></q></dt></tr></i><div id='JTLfG'><tfoot id='JTLfG'></tfoot><dl id='JTLfG'><fieldset id='JTLfG'></fieldset></dl></div>
                    <bdo id='JTLfG'></bdo><ul id='JTLfG'></ul>

                • <tfoot id='JTLfG'></tfoot>

                    <legend id='JTLfG'><style id='JTLfG'><dir id='JTLfG'><q id='JTLfG'></q></dir></style></legend>