Hi Matius,
Thank you for your clue, I have modified my code and it run exactly
like I want.
My code as following below:
def my_view(request):
username = []
password = []
if request.method == "POST" :
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
render_to_response ('login.html', {'username': username,
'password' : password})
if user is not None:
if user.is_active:
login(request,user)
return render_to_response('success.html',
{'username':username})
else:
return HttpResponse('disabled account')
else:
return HttpResponse('invalid login')
else:
return render_to_response ('login.html', {'username': username,
'password' : password})
the login.html and success.html remain the same
once again thanks man and have a nice day :)
regards,
-vierda-
On Dec 16, 8:50 am, brandonh <[email protected]> wrote:
> I just use the get method on the POST object.
>
> username = request.POST.get('username','')
>
> if there is no username posted, the username will be blank and should
> not authenticate. same for the password.
>
> On Dec 16, 9:39 am, Matias <[email protected]> wrote:
>
> > Hello,
>
> > You should show the login if the method != "POST".
> > By calling render_to_response(...) it will render your template, so that is
> > what the view must return.
>
> > I think you want something like this:
>
> > def my_view(request):
> > if request.method == "POST" :
> > username = request.POST['username']
> > password = request.POST['password']
> > user = authenticate(username=username, password=password)
>
> > if user is not None:
> > if user.is_active:
> > login(request,user)
> > return render_to_response('success.html',
> > {'username':username})
> > else:
> > return HttpResponse('disabled account')
> > else:
> > return HttpResponse('invalid login')
> > else:
> > *return render_to_response* ('login.html', {'username': username,
> > 'password' : password})
>
> > Regards,
> > Matias.
>
> > On Tue, Dec 16, 2008 at 1:17 AM, vierda <[email protected]> wrote:
>
> > > Hi Matias,
>
> > > Thanks for your reply but I did'nt get your point. sorry I'm newbie
> > > and still learning.
> > > I modified my_view function in my views as your suggestion (if I
> > > didn't wrong get your point) as per below:
>
> > > def my_view(request):
> > > if request.method == "POST" :
> > > username = request.POST['username']
> > > password = request.POST['password']
> > > user = authenticate(username=username, password=password)
> > > render_to_response ('login.html', {'username': username,
> > > 'password' : password})
>
> > > if user is not None:
> > > if user.is_active:
> > > login(request,user)
> > > return render_to_response('success.html',
> > > {'username':username})
> > > else:
> > > return HttpResponse('disabled account')
> > > else:
> > > return HttpResponse('invalid login')
> > > else:
> > > return HttpResponse ('home page')
>
> > > above code run and shows home page text, I assume that the method is
> > > not POST regarding the result.
> > > What sholud I do to fix this and show my login page. somebody please
> > > help me to understand this concept. Thank you
>
> > > regards,
> > > -Mila-
>
> > > On Dec 15, 9:06 am, Matias <[email protected]> wrote:
> > > > Hello,
>
> > > > You should check by which method was called your view.
> > > > something like
>
> > > > if request.method == "POST":
> > > > #now you can use request.POST
> > > > else:
> > > > #just show the template
>
> > > > Hope that helps,
>
> > > > Matias.
>
> > > > On Mon, Dec 15, 2008 at 5:15 AM, vierda <[email protected]> wrote:
>
> > > > > Dear Ronny/all,
>
> > > > > Thank you for your reply. I have modified my code and the error still
> > > > > occur.
> > > > > my new code as following below:
>
> > > > > def my_view(request):
> > > > > username = request.POST['username']
> > > > > password = request.POST['password']
> > > > > user = authenticate(username=username, password=password)#create
> > > > > Authentication object
> > > > > render_to_response ('login.html', {'username': username,
> > > > > 'password' : password})
>
> > > > > if user is not None:
> > > > > if user.is_active:
> > > > > login(request,user)
> > > > > return render_to_response('success.html',
> > > > > {'username':username})
> > > > > else:
> > > > > return HttpResponse('disabled account')
> > > > > else:
> > > > > return HttpResponse('invalid login')
>
> > > > > and my login.html as per below:
> > > > > <form action="/login/" method="POST">
>
> > > > > Enter Username: <input type=text name="username">
> > > > > Enter Password: <input type=password name="password">
> > > > > <input type="submit" Value="Login"/>
> > > > > </form>
>
> > > > > my success.html as per below :
> > > > > <html>
> > > > > <h1> welcome{{username}}</h1>
> > > > > </html>
>
> > > > > somebody kindly help and thank you in advance
>
> > > > > On Dec 6, 12:05 am, "Ronny Haryanto" <[email protected]> wrote:
> > > > > > On Sat, Dec 6, 2008 at 2:42 PM, vierda <[email protected]> wrote:
> > > > > > > today I try to make login page with follow example code in
> > > > > > > djangoproject (user authentication chapter), code as following
> > > below :
>
> > > > > > > def my_view(request):
> > > > > > > username = request.POST['username']
> > > > > > > password = request.POST['password']
> > > > > > > user = authenticate(username=username,
> > > > > > > password=password)#create
> > > > > > > Authentication object
>
> > > > > > > if user is not None:
> > > > > > > if user.is_active:
> > > > > > > login(request,user)
> > > > > > > return HttpResponse('login success')
> > > > > > > else:
> > > > > > > return HttpResponse('disable account')
> > > > > > > else:
> > > > > > > return HttpResponse('invalid login')
>
> > > > > > > the above code always shows MultiValueDictKeyError with exception
> > > > > > > value "Key 'username' not found in <QueryDict: {}>".
>
> > > > > > That exception will always be raised if the view is called via GET
> > > > > > method instead of POST.
>
> > > > > > If you're sending it via POST, then the information you provided was
> > > > > > not sufficient. We need to see template code that renders the form
> > > (or
> > > > > > the html form if you're not using templates) and probably the form
> > > > > > as
> > > > > > well.
>
> > > > > > Ronny
>
> > > > --
> > > > :wq
>
> > --
> > :wq
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---