Problem with a view - MultiValueDictKeyError

960 views
Skip to first unread message

Dariusz Mysior

unread,
Dec 17, 2015, 4:16:33 PM12/17/15
to Django users
I have this view and I have a bug why?

def index(request):
   
# jezeli formularz coś wysłał
    if request.method == 'POST':
        form_r
= UserCreationForm(request.POST)
        username
= request.POST['username']
        password
= request.POST['password']
        user
= authenticate(username=username, password=password)

       
# jezeli uzytkownik istnieje
        if user is not None:
           
if user.is_active:
                login
(request, user)
               
return HttpResponseRedirect('profile')

           
else:
               
# Return a 'disabled account' error message
                ...
       
else:
           
# Return an 'invalid login' error message.
            return HttpResponseRedirect('invalid_login')
       
if form_r.is_valid():
            form_r
.save()
            info
="Zarejestrowałeś się!"
            return render_to_response('ownsite/register_success.html', {info:'info'})
       
else:
           
return HttpResponseRedirect('/')
   
# tworze formularze
    args = {}
    args
.update(csrf(request))
    args
['form_l'] = AuthenticationForm()
    args
['form_r']= UserCreationForm()

   
return render_to_response('index.html', args)


A bug


MultiValueDictKeyError at /

"'password'"
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.4
Exception Type: MultiValueDictKeyError
Exception Value:
"'password'"
Exception Location: C:\Python34\lib\site-packages\django\utils\datastructures.py in __getitem__, line 322
Python Executable: C:\Python34\python.exe
Python Version: 3.4.3
Python Path:
['C:\\Python34\\ownsite',
 'C:\\WINDOWS\\SYSTEM32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Python34\\lib\\site-packages']
Server time: Czw, 17 Gru 2015 22:07:11 +0100

Ezequiel Bertti

unread,
Dec 17, 2015, 6:20:16 PM12/17/15
to django...@googlegroups.com

On Thu, Dec 17, 2015 at 7:16 PM, Dariusz Mysior <mysior...@gmail.com> wrote:
password = request.POST['password']

Maybe your form is not a input with 'password'

Can you show your HTML code builded by django


--
Ezequiel Bertti
E-Mail: ebe...@gmail.com
Cel: (21) 99188-4860

Galia Ladiray

unread,
Dec 18, 2015, 12:49:20 AM12/18/15
to Django users
We don't have your UserCreationForm, but usually those things have password1 and password2 fields, and no "password"

Dariusz Mysior

unread,
Dec 18, 2015, 2:22:54 PM12/18/15
to Django users
My template looks like below but I changed someting in View and than I had this error

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
   
<title>{% block title %}{% endblock %}</title>
   
<link rel="stylesheet" type="text/css" href="{% static 'ownsite/css/style.css' %}" />


</head>
<body>

<div id="header">
{% block header %}
    {% if user.is_authenticated %}
       
<p>Jesteś  zalogowany <a href='profile'>{{ user.username }}</a></p>
       
<p><a href='/logout_view'>wyloguj</a></p>
   
{% else %}
       
<h2>Login</h2>
       
<form action = '/' method = 'post'>{% csrf_token %}
        {{ form_l.as_p }}
       
<input type='submit' value='Log' />
       
</form>
    {% endif %}
{% endblock %}
</div>
<div id="content">
{% block content %}
   
<div id="left_content">
        {% block left_content %}
            {% if user.is_authenticated %}
               
<p>Jesteś zalogowany</p>
            {% else %}
               
<p>Zaloguj się lub zarejestruj</p>
            {% endif %}
        {% endblock %}
   
</div>
   
<div id="right_content">
    {% block right_content %}
        {% if user.is_authenticated %}
           
<p>Jesteś zalogowany</p>
        {% else %}
           
<h2>Register</h2>
           
<form action = '/' method = 'post'>{% csrf_token %}
            {{form_r.as_p}}
             
<input type='submit' value='Register' />
             
</form>
        {% endif %}
    {% endblock %}
{% endblock %}
   
</div>
</div>
<div id="footer">
<p>copyright &copy; Dariusz Mysior</p>
</div>

</body>
</html>

Dariusz Mysior

unread,
Dec 18, 2015, 4:28:38 PM12/18/15
to Django users
Hmm I split index view on two views index and register_user and now it is ok heh.


W dniu czwartek, 17 grudnia 2015 22:16:33 UTC+1 użytkownik Dariusz Mysior napisał:
Reply all
Reply to author
Forward
0 new messages