django 1.11 login user

369 views
Skip to first unread message

sum abiut

unread,
May 4, 2017, 7:04:09 PM5/4/17
to django...@googlegroups.com
Hi,
 i have try to login user using the guide from https://docs.djangoproject.com/en/1.11/topics/auth/default/

but get an error. Can someone advise what i am doin wrong here



he is my view.py

#Authentication user
def login(request):
    username=request.POST['username']
    password=request.POST['password']
    user=authenticate(request,username=username,password=password)
    if user is not None:
        login(request,user)
        #Redirect to dashboard page
        return render(request,'dashboard.html')
    else:
        # return invalid login message
        return HttpResponse( 'You have enter a wrong password or username please again')



Error message:


Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/login/

Django Version: 1.11
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'fundmanager']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/root/.virtualenvs/benchmark/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/root/.virtualenvs/benchmark/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/root/.virtualenvs/benchmark/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/www/projects/benchmark/fundmanager/views.py" in login
  22.     username=request.POST['username']

File "/root/.virtualenvs/benchmark/local/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__
  85.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /login/
Exception Value: "u'username'"

ludovic coues

unread,
May 5, 2017, 2:13:34 AM5/5/17
to django...@googlegroups.com
My guess is that your error happen when accessing the page before submitting the firm. So you don't have any POST data and trying to access any key rise an error.

You can use request.POST.get("key", "default_value") or you can write an if and testing that request.method is "POST".

If I'm wrong, I suggest you share your template with your form.

A few tips for your future with django, not really related to your problem. First, django provide a Form API which can handle form creation and validation.
The doc is here
And django already provide a few of these for authentication, the doc for the login form is here:

If you have trouble to understand​ how to use the form API, I recommend the django girls tutorial on the subject. That helped me a lot.

A next step would be to look at class based view and check if there is one for login. I've heard about that but never looked. If that true, you might be able to get your view, as simply as declaring a class inheriting from the django login view and putting a template attribute in your class pointing to your template file

Anyway, good luck with your issue 

--
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...@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/CAPCf-y4Lg1ZkqDA4TR7RuV6z-gutuKmzFZ%2BvpQsr%3DUnqXV2OkA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
May 8, 2017, 11:29:29 AM5/8/17
to django...@googlegroups.com

On Friday 05 May 2017 10:03:47 sum abiut wrote:

 

> i have try to login user using the guide from

> https://docs.djangoproject.com/en/1.11/topics/auth/default/

>

> but get an error. Can someone advise what i am doin wrong here

 

 

It's a flaw in the docs.

It assumes that by now you are familiar with function-based views and how to write them for forms processing.

 

The code below splits the GET part (rendering the form) and the POST part (processing the form) and you're only shown the POST part. It assumes you already have another view function that renders the form or that the login form is part of many other pages.

 

> he is my view.py

>

> #Authentication user

> def login(request):

> username=request.POST['username']

> password=request.POST['password']

> user=authenticate(request,username=username,password=password)

> if user is not None:

> login(request,user)

> #Redirect to dashboard page

> return render(request,'dashboard.html')

> else:

> # return invalid login message

> return HttpResponse( 'You have enter a wrong password or

> username please again')

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages