尝试登录后的MultiValueDictKeyError

时间:2016-09-07 09:14:51

标签: python django

我收到MultiValueDictKey错误。

这是我的观点:

from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.http import HttpResponse, HttpResponseRedirect
from bookonshelf import settings

def Login(request):
    next = request.GET.get('next', '/home/')
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']     
        user = authenticate(username=username, password=password)

    if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect(next)
            else:
                return HttpResponse("Inactive user.")
        else:
            return HttpResponseRedirect(settings.LOGIN_URL)

    return render(request, "index/login.html", {'redirect_to': next})

我的login.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head     content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">

<title>Admin panel</title>

<!-- Bootstrap core CSS -->
<link href="/static/css/bootstrap.min.css" rel="stylesheet">

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="/static/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="/static/css/signin.css" rel="stylesheet">

<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-   warning.js"></script><![endif]-->
<script src="/static/assets/js/ie-emulation-modes-warning.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media  queries -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"> </script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

<div class="container">

  <form class="form-signin" method="post" action=".?next={{ redirect_to }}"> {% csrf_token %}
    <h2 class="form-signin-heading">Admin panel</h2>
    <label for="username" class="sr-only">Username</label>
    <input type="text" id="username" class="form-control" placeholder="Username" required autofocus>
    <label for="password" class="sr-only">Password</label>
    <input type="password" id="password" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>

</div> <!-- /container -->


<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="/static/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>

在网上阅读了一些类似的问题之后,我已经尝试将request.POST更改为request.POST.get,但这对我不起作用。

def Login(request):
    next = request.GET.get('next', '/home/')
    if request.method == "POST":
        username = request.POST.get('username', False)
        password = request.POST.get('password', False)
        user = authenticate(username=username, password=password)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您永远不会为您的输入命名,因此它们永远不会添加到帖子数据

<input type="text" id="username" name="username">
<input type="password" id="password" name="password">

我删除了一些简洁的属性

另请注意:

  • 您应该从django.conffrom django.conf import settings)导入设置,这不是一个模块,它是一个能够始终获得正确的DJANGO_SETTINGS_MODULE魔法的类。 / p>

  • 您应该使用django表单来阻止这类错误的发生

  • 您应该提供比False更好的默认值,这些值是字符串,而False在这里没有意义。

答案 1 :(得分:1)

为这两个字段添加name=。实施例

<input type="text" name="username" id="username" class="form-control" placeholder="Username" required autofocus>