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
https://docs.djangoproject.com/en/1.11/ref/forms/api/
And django already provide a few of these for authentication, the doc for
the login form is here:
https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.forms.AuthenticationForm

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

On 5 May 2017 1:03 am, "sum abiut" <suab...@gmail.com> wrote:

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-packa
ges/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/root/.virtualenvs/benchmark/local/lib/python2.7/site-packa
ges/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-packa
ges/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-packa
ges/django/utils/datastructures.py" in __getitem__
  85.             raise MultiValueDictKeyError(repr(key))

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

-- 
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+unsubscr...@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/ms
gid/django-users/CAPCf-y4Lg1ZkqDA4TR7RuV6z-gutuKmzFZ%2BvpQsr
%3DUnqXV2OkA%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAPCf-y4Lg1ZkqDA4TR7RuV6z-gutuKmzFZ%2BvpQsr%3DUnqXV2OkA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@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/CAEuG%2BTb%3DzQktCUen5iRfLi2tGm2y6iXbMEuOL8NHHetGR%2B-atg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to