为什么我的Django/Bootstrap项目中总是出现多值dictkey错误?

2024-09-29 19:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我的登录表单的代码:

<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表单代码中包含该字段的名称,但每次尝试使用数据库中已有的凭据时,仍会继续出现错误。有什么想法吗


Tags: 代码divform表单inputrequestcustomgroup
1条回答
网友
1楼 · 发布于 2024-09-29 19:21:06

您应该将html表单Logina href更新为我给出的按钮

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

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

另请参阅get method dictionary生成此错误not Actual get data如果您希望在数据未通过请求时获取默认数据获取默认值get('key name',default value)

所以需要更新我在a hrefbutton中给出的内容,然后告诉我发生了什么

相关问题 更多 >

    热门问题