Friday, December 18, 2015

Re: Problem with a view - MultiValueDictKeyError

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>



W dniu czwartek, 17 grudnia 2015 22:16:33 UTC+1 użytkownik Dariusz Mysior napisał:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3a3e2695-c20d-4996-8464-82cfee72a81c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment