-2

I'm getting a MultiValueDictKey error.

This is my view:

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})

And my 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>

I already tried changing request.POST to request.POST.get after reading some similar problems online, but that's not working for me.

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)

How do I solve this?

Jen Jensen
  • 141
  • 1
  • 1
  • 7

2 Answers2

1

You never give your inputs a name so they are never added to the post data

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

I removed some attributes for brevity

Also note:

  • You should import settings from django.conf (from django.conf import settings), this isn't a module, its a class that does magic to always get the right DJANGO_SETTINGS_MODULE.

  • You should just use a django form which can stop these kind of errors from ever occuring

  • You should give better defaults than False, these values are strings and False doesn't make sense here.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Thanks for the comment, I added names to both username and password. It doesn't give me a MultiValueDictKeyError anymore, but it just keeps me on the page now.. – Jen Jensen Sep 07 '16 at 09:34
  • @JenJensen - If you have another question you should consider raising it as a new question with what you have tried/researched. If any of these answers solved this problem, you should consider accepting it – Sayse Sep 07 '16 at 09:35
1

Put name= for both fields. Ex

<input type="text" name="username" id="username" class="form-control" placeholder="Username" required autofocus>
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • Thanks for the comment, I added names to both username and password. It doesn't give me a MultiValueDictKeyError anymore, but it just keeps me on the page now.. – Jen Jensen Sep 07 '16 at 09:34