2016-09-07 4 views
-1

MultiValueDictKey 오류가 발생합니다. 로그인 시도 후 MultiValueDictKeyError

내이다 :

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 : 이미 온라인 몇 가지 비슷한 문제를 읽은 후 request.POST.get하는 request.POST를 변경 시도

<!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> 

하지만, 그건 나를 위해 작동하지 않습니다.

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) 

어떻게 해결할 수 있습니까?

+0

귀하의 login.html – itzMEonTV

+1

및 .get으로 변경했을 때 사용한 코드를 게시하십시오. 또한 이런 종류의 시나리오를 다루는 데 특별히 필요한 장고 (Django) 양식을 사용하지 않는 이유를 설명하십시오. –

+0

가능한 [django MultiValueDictKeyError 오류의 중복, 어떻게 처리합니까] (http://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – Sayse

답변

1

가 포스트 데이터에 추가되지 않습니다 그래서 내가 또주의 간결

에 대한 몇 가지 속성을 제거

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

당신은 당신의 입력에 이름을주지 않을 :

  • django.conf ()에서 설정을 가져와야합니다.), 이것은 모듈이 아니며, 마법을 사용하여 항상 올바르게 얻을 수있는 클래스입니다. DJANGO_SETTINGS_MODULE.

  • 그냥 지금 당신은 False보다 더 나은 기본값을 제공한다

  • 을 발생하는에서 오류 이런 종류의를 중지 할 수 있습니다 장고 양식을 사용한다,이 값은 문자열과 거짓 여기에 이해가되지 않습니다.

+0

댓글을 주셔서 감사합니다. 사용자 이름과 비밀번호에 이름을 추가했습니다. 더 이상 MultiValueDictKeyError를주지는 못하지만 지금은 페이지에 계속 머물러 있습니다 .. –

+0

@JenJensen - 다른 질문이 있으면 시도한 것과 함께 새로운 질문으로 제기하는 것이 좋습니다. 이 문제가 해결 된 경우 – Sayse

1

두 필드 모두에 대해 name=을 넣습니다. Ex

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

의견을 보내 주셔서 감사 드리며 사용자 이름과 비밀번호에 모두 이름을 추가했습니다. 더 이상 MultiValueDictKeyError를주지는 못하지만, 지금은 페이지에 계속 있습니다. –