El objeto de solicitud HTTP no responde

I am making a login form in Django. When I am click on login submit button , It gives an error of

ValueError at /registration/ The view registration.views.login didn't return an HttpResponse object.

The view file is

from django.template import  loader
from django.shortcuts import render
from registration.models import Registration
from django.http import HttpResponse
def login(request):
  if request.method == 'POST':
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
      if user.is_active:
        login(request, user)
        # success
        return HttpResponseRedirect('sucess')
        # disabled account
        return direct_to_template(request, 'inactive_account.html')
      # invalid login
      return direct_to_template(request, 'invalid_login.html')

def logout(request):
  return direct_to_template(request, 'logged_out.html')

and login.html file is

<form action="/login/" method="post" accept-charset="utf-8">
<label for="username">Username:--</label><input type="text" name="username" value="" id="username" /><br>
<label for="password">Password:-- </label><input type="password" name="password" value="" id="password" />
<p><input type="submit" value="Login →"></p>

preguntado el 28 de agosto de 12 a las 12:08

Two things I see - What happens if the request is GET? You don't return anything, and sucess está mal escrito. -

@yeah Burhan is right but normally direct_to_template used from urls.py m i rit ? -

3 Respuestas

Your login view will return a HttpResponse for a POST request only (on form submit). When you address your browser to login page, it makes a GET request, which is not handled in your view

I`ll recomend you to read more about user athentication: doc.

And as mentioned, use render or render_to_response funciones from django.shortcuts

Respondido 28 ago 12, 19:08

En lugar de direct_to_template es posible que desee utilizar render_to_response which renders a given template and returns HttpResponse.

Remitir render_to_response. Note; you will have to import it using from django.shortcuts import render_to_response

Respondido 28 ago 12, 12:08

after suggestion and modification now i am getting MultiValueDictKeyError at /registration/ "Key 'user_name' not found in <QueryDict: {}>" - user786

Why not you are using render_to_response ?

The answer of your question is you are not returning the Httpresponse objeto

If your login is successful then only you will be redirect to success .

Prueba esta

 return render_to_response(your_template_name, 

Don't forget to import context_instance like

from django.template import RequestContext

Respondido 28 ago 12, 12:08

MultiValueDictKeyError at /registration/ "Key 'user_name' not found in <QueryDict: {}> because you are not passing the user_name in your dict however your current problem is solved better to ask a new question - user1614526

remove value = "" from input fields then try - user1614526

you should have to refer doc first anyway send me ur mail id i am sending you script - user1614526

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.