View didn't return an HttpResponse object

116 views
Skip to first unread message

Dariusz Mysior

unread,
Oct 7, 2014, 2:37:49 PM10/7/14
to django...@googlegroups.com
I have form to change password like below, and when I try to display it I have an bug:

ValueError at /password_change/


 
The view dom.views.password_change_dom didn't return an HttpResponse object.

 

   
     
     
   
   
     
     
   

   
     
     
   

   
       
     
Request Method:GET
Request URL:http://darmys.pythonanywhere.com/password_change/
Django Version:1.6.5
Exception Type:ValueError
Exception Value:
The view dom.views.password_change_dom didn't return an HttpResponse object.

My forms.py is:
class FormularzPasswordChange(forms.Form):
    password1
= forms.CharField(label="Hasło:",widget=forms.PasswordInput())
    password2
= forms.CharField(label="Powtórz hasło:",widget=forms.PasswordInput())

   
def clean_password2(self):
        password1
= self.cleaned_data['password1']
        password2
= self.cleaned_data['password2']
       
if password1 == password2:
               
return password2
       
else:
               
raise forms.ValidationError("Hasła się różnią")



urls.py

url(r'^password_change/$', views.password_change_dom, name='change_password')

change_password.html

{% extends "base_site_dom.html" %}

{% block title %} - zmiana hasła {% endblock %}

{% block content %}

<center>
   
<h2>Zmiana hasła na nowe.</h2>
   
Wpisz nowe hasło 2 razy.
   
<p>
       
<div style="margin-left:35%; margin-right:35%;">
           
<fieldset>
               
<form method="post" action=".">{% csrf_token %}
               
{{ form.as_p }}
               
<input type="submit" value="Zmień"> <input type="reset" value="Wartoci początkowe">
               
</form>
            </
fieldset>
       
</div>
    </
center>
   
{% endblock %}
   
{% block footer %}
   
<p>
   
<div style="text-align:center;"><a href="/">Strona głóna</a></div>
   
</p>
   
{% endblock %}


Collin Anderson

unread,
Oct 7, 2014, 3:07:35 PM10/7/14
to django...@googlegroups.com
What does your password_change_dom view look like?

Dariusz Mysior

unread,
Oct 7, 2014, 3:11:09 PM10/7/14
to django...@googlegroups.com
It's :

ef password_change_dom(request):
       
if request.method == 'POST':
            form
= FormularzPasswordChange(request.POST)
           
if form.is_valid():
                user
= authenticate(username=form.cleaned_data['username'])
             
#user = User.objects.get(username='username')
                user
.set_password(form.cleaned_data['password2']),
                user
.save()
                login
(request,user)
               
template = loader.get_template("registration/change_password.html")
                variables
= RequestContext(request,{'user':user})
                output
= template.render(variables)
               
return HttpResponseRedirect("/")
           
else:
                form
= FormularzPasswordChange()
               
template = loader.get_template('registration/change_password.html')
                variables
= RequestContext(request,{'form':form})
                output
= template.render(variables)
               
return HttpResponse(output)

Collin Anderson

unread,
Oct 7, 2014, 3:17:14 PM10/7/14
to django...@googlegroups.com
unindent the last bit:

Collin Anderson

unread,
Oct 7, 2014, 3:21:16 PM10/7/14
to django...@googlegroups.com
also, I highly recommend the render() shortcut:

def password_change_dom(request):

   
if request.method == 'POST':
        form
= FormularzPasswordChange(request.POST)
       
if form.is_valid():
            user
= authenticate(username=form.cleaned_data['username'])

            user
.set_password(form.cleaned_data['password2'])
            user
.save()
            login
(request, user)

           
return HttpResponseRedirect("/")
   
else:
        form
= FormularzPasswordChange()

   
return render(request, 'registration/change_password.html', {'form': form})

Dariusz Mysior

unread,
Oct 7, 2014, 3:31:29 PM10/7/14
to django...@googlegroups.com
It was like You said, now I see form and when I write password and submit I have

KeyError at /password_change/


 
'username'

 

   
     
     
   
   
     
     
   

   
     
     
   

   
     
     
   


   
     
     
   


   
     
     
Request Method:POST
Request URL:http://darmys.pythonanywhere.com/password_change/
Django Version:1.6.5
Exception Type:KeyError
Exception Value:
'username'
Exception Location:/home/darmys/dom/dom/views.py in password_change_dom, line 55

Collin Anderson

unread,
Oct 7, 2014, 3:39:39 PM10/7/14
to django...@googlegroups.com
are they already logged in? use request.user.set_password() and request.user.save()

Dariusz Mysior

unread,
Oct 7, 2014, 4:05:31 PM10/7/14
to django...@googlegroups.com
It work's! :)

Thank You!!

 now I have:

def password_change_dom(request):
       
if request.method == 'POST':
            form
= FormularzPasswordChange(request.POST)
           
if form.is_valid():

                request
.user.set_password(form.cleaned_data['password2'])
                request
.user.save()

               
template = loader.get_template("registration/change_password.html")

                variables
= RequestContext(request,{'user':request.user})
                output
= template.render(variables)

               
return HttpResponseRedirect("/")
       
else:
                form
= FormularzPasswordChange()
               
return render(request, 'registration/change_password.html', {'form': form})

Dariusz Mysior

unread,
Oct 20, 2014, 7:46:27 AM10/20/14
to django...@googlegroups.com
Now I try change this part of code in views.py

#request.user.set_password(form.cleaned_data['password2'])
               
#request.user.save()
               
#return render(request, 'registration/change_password_success.html', {'user': request.user})

On

from django.contrib.auth import logout,login,authenticate,password_change
.
.
.

password_change
(request,'registration/change_password_success.html',post_change_redirect=None,
                    password_change_form
=FormularzPasswordChange, current_app=None, extra_context=None)

urls.py

This change

#url(r'^password_change/$', views.password_change_dom, name='change_password'),


On this

url(r'^password_change/$', 'django.contrib.auth.views.change_password'),

And I have message on first site :/


ImportError at /

cannot import name password_change
Request Method: GET
Request URL: http://darmys.pythonanywhere.com/
Django Version: 1.6.5
Exception Type: ImportError
Exception Value:
cannot import name password_change
Exception Location: /home/darmys/dom/dom/views.py in <module>, line 7



W dniu wtorek, 7 października 2014 20:37:49 UTC+2 użytkownik Dariusz Mysior napisał:

Collin Anderson

unread,
Oct 20, 2014, 5:55:28 PM10/20/14
to django...@googlegroups.com
Hello,

from django.contrib.auth import logout,login,authenticate,password_change
I think you want:
from django.contrib.auth import logout,login,authenticate
from django.contrib.auth.views import change_password

Collin

Dariusz Mysior

unread,
Oct 21, 2014, 4:01:25 AM10/21/14
to django...@googlegroups.com
Thank You, once again it help :)

Dariusz Mysior

unread,
Oct 24, 2014, 12:17:25 PM10/24/14
to django...@googlegroups.com
I have in views.py

message="Password was changed"
return logout_then_login(request, login_url='/login/',extra_context={'message': message})


And I want display this message in

login.html below

{% extends "base_site_dom.html" %}
{% block title %} - logowanie {% endblock %}

{%  block content %}
<center>
<h2> Logowanie na stronie Managera Piłkarskiego GoalKick </>
<p>
<p>{{ message}}</p>

<div style="margin-left:35%;margin-right:35%;">
<fieldset>
<form method="post" action=".">{%  csrf_token %}
<div style="text-align:left";>{{ form.as_p }}</div>
<input type="hidden" name="next" value="/" />
<input type="submit" value="Logowanie"/><input type="reset" value="Wartości domyślne" />

</form>
</
fieldset>
</div>
</
center>
{% endblock %}
{% block footer %}
<p>
<div style="text-align:center;"><a href="/">Strona główna</a></div>
</p>
{% endblock %}

What I do wrong, this message is not show :/


W dniu wtorek, 7 października 2014 20:37:49 UTC+2 użytkownik Dariusz Mysior napisał:

Collin Anderson

unread,
Oct 27, 2014, 8:16:14 AM10/27/14
to django...@googlegroups.com
Hello,

The messages framework might be helpful here.

from django.contrib import messages
messages
.add_message(request, messages.INFO, 'Password was changed')
return logout_then_login(request, login_url='/login/')

<h2> Logowanie na stronie Managera Piłkarskiego GoalKick </>
<p>

{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}


Collin
Reply all
Reply to author
Forward
0 new messages