【问题标题】:Why do I keep getting a multiValueDictKey Error in my Django/Bootstrap project?为什么我的 Django/Bootstrap 项目中不断出现 multiValueDictKey 错误?
【发布时间】:2020-06-04 00:08:38
【问题描述】:

这是我的登录表单的代码:

<form class="user" action="/returningAgent/" method="post" >
                <div class="form-group">
                  <input type="email" class="form-control form-control-user" id= "InputEmail" name="InputEmail" aria-describedby="emailHelp" placeholder="Enter Email Address...">
                </div>
                <div class="form-group">
                  <input type="password" class="form-control form-control-user" id="InputPassword" name="InputPassword" placeholder="Password">
                </div>
                <div class="form-group">
                  <div class="custom-control custom-checkbox small">
                    <input type="checkbox" class="custom-control-input" id="customCheck">
                    <label class="custom-control-label" for="customCheck">Remember Me</label>
                  </div>
                </div>
                <a href="/returningAgent/" class="btn btn-primary btn-user btn-block">
                  Login
                </a>
                <hr>
                <a href="/returningAgent/" class="btn btn-google btn-user btn-block">
                  <i class="fab fa-google fa-fw"></i> Login with Google
                </a>
                <a href="/returningAgent/" class="btn btn-facebook btn-user btn-block">
                  <i class="fab fa-facebook-f fa-fw"></i> Login with Facebook
                </a>
</form>

这是表单提交触发的代码:

def returningAgent(request):
 #try:
   x = Agent.objects.get(bizEmail = request.POST["InputEmail"], password = request.POST["InputPassword"])  
   diction = {
        'f' : x.firstName,
        'l' : x.lastName,
        'e' : x.bizEmail
        }
   return render(request, 'index.html', diction)
 #except:
    #return HttpResponseRedirect('/404/')

我已尝试将 request.POST 切换为 request.POST.get,并在 HTML 表单代码中包含字段的名称,但每次尝试使用数据库中已有的凭据时,我仍然会继续收到错误消息.有什么想法吗?

【问题讨论】:

    标签: python django bootstrap-4 multi-value-dictionary


    【解决方案1】:

    你应该更新你的html表单登录a href到我给的按钮

     <button type="submit" class="btn btn-primary btn-user btn-block">
           Login
     </button>
    

    您实际上在request.POST.get('InputEmail') 中遇到的错误如果您在视图中打印,则会收到此错误,因为数据没有让您在登录时使用href,因此它将获得请求而不是实际提交表单

    如果你想在数据不通过请求时获取默认数据获取默认值get('key name',default value)

    所以需要更新我在a href 中给button 然后让我发生了什么事

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 2021-08-29
      • 2020-07-27
      • 2020-11-08
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多