Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding and customizing "django.contrib.auth.views.login"

Tags:

python

django

I'm trying to add a "keep me logged in" check box to Django's default login view.

Here is what I'm doing:

urls.py

url(r'^login/$',
    myuser_login,
    {'template_name': 'app_registration/login.html', 'authentication_form': MyAuthenticationForm},
    name='auth_login',
),

views.py

from django.contrib.auth.views import login

def myuser_login(request, *args, **kwargs):
    if request.method == 'POST':
        if not request.POST.get('remember', None):
            request.session.set_expiry(0)

    login(request, *args, **kwargs)

So basically, I'm trying to add additional information to my own view and just simply call Django's default login function. When I do this, I get this error:

ValueError at /accounts/login/
The view app_registration.views.myuser_login didn't return an HttpResponse object.

I checked the Django source code, and the default contrib.auth.views.login function obviously returns an HttpResponse.

What should I do :(((?

Thanks!!

like image 237
user2492270 Avatar asked Feb 15 '23 14:02

user2492270


1 Answers

All django views must return HttpResponse. Your view isn't returning anything.

You should return like this

def myuser_login(request, *args, **kwargs):
    if request.method == 'POST':
        if not request.POST.get('remember', None):
            request.session.set_expiry(0)

    return login(request, *args, **kwargs)
like image 128
user710907 Avatar answered Feb 23 '23 11:02

user710907