These are chat archives for django/django

7th
Nov 2016
Miracle Ayodele
@iammiracle
Nov 07 2016 03:06
hi guys
i have been working on login and my function seems not to do anything, no error also

var code = "def user_login(request):

context = {}
if request.method == 'POST':
    email = request.POST['email']
    password = request.POST['password']
    user = authenticate(email=email, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            # return "Correct"
            return HttpResponseRedirect('/home/')
        else:
            # return "InCorrect"
            return HttpResponseRedirect('/index/')
    else:
        # return "Wrong"
        return HttpResponseRedirect('/home/')
else:
    return render_to_response('index.html', {}, context)

";

the registration works well
probably they are not communicating well
var code = "def Registration(request):
context = {}
form = RegistrationForm(data=request.POST)
if request.POST:
    if form.is_valid():
        new_member = form.save()
        # return render_to_response('registration/success.html',)
        return HttpResponseRedirect('/home/')
context.update({'form': form})
return render(request, 'index.html', context)";
raghu
@raghuraju
Nov 07 2016 05:42
Hi guys
Miracle Ayodele
@iammiracle
Nov 07 2016 15:08
anyone online?
Filip Figiel
@megapctr
Nov 07 2016 15:09
sup
@koderNeptune you could show your urls.py
Miracle Ayodele
@iammiracle
Nov 07 2016 15:10
@megapctr i have been even trying out some stuffs
check out my view again

var code = "def user_login(request):

context = {}
print (request.user.is_authenticated())
if request.method == 'POST':
    email = request.POST['email']
    password = request.POST['password']
    user = authenticate(email=email, password=password)
    if user:
        if user.is_active:
            login(request, user)
            print (request.user.is_authenticated())
            print user
            return HttpResponseRedirect('/home/')
        else:
            print (request.user.is_authenticated())
            print user
            return HttpResponse("Your Account is disabled")
    else:
        print (request.user.is_authenticated())
        print "Invalid login details: {0}, {1}".format(email, password)
        return HttpResponse("Invalid login details supplied")
else:
    print (request.user.is_authenticated())
    return render(request, 'index.html', context)

";

the message i get is return HttpResponse("Invalid login details supplied")
Filip Figiel
@megapctr
Nov 07 2016 15:11
i see
Miracle Ayodele
@iammiracle
Nov 07 2016 15:11
either i put the right details or not
Filip Figiel
@megapctr
Nov 07 2016 15:11
can you add print request.POST below if request.method == 'POST':?
Miracle Ayodele
@iammiracle
Nov 07 2016 15:12
yeah
got <QueryDict: {u'csrfmiddlewaretoken': [u'6lb3P6xQ3FfIqTlrUh4xn4UyLBwC8amyojLENOBfThXUja604gHQjfYfQyRoRsYi'], u'password': [u'mole'], u'email': [u'mole@gmail.com']}>
Filip Figiel
@megapctr
Nov 07 2016 15:16
looks ok :|
what if you run ./manage.py shell
then import your User model and try to run u = User.objects.get(email='mole@...') and u.check_password('mole')
Miracle Ayodele
@iammiracle
Nov 07 2016 15:20
True o
it gave me True
Filip Figiel
@megapctr
Nov 07 2016 15:21
this is really weird, it should work
Miracle Ayodele
@iammiracle
Nov 07 2016 15:28
i am surprised too
i should be getting return HttpResponseRedirect('/home/')
and not return HttpResponse("Invalid login details supplied")
Filip Figiel
@megapctr
Nov 07 2016 15:28
yep
one more thing you could try is to print email and password before calling authenticate
Miracle Ayodele
@iammiracle
Nov 07 2016 15:32
i have my correct username and password
i think i get where the problem is
Filip Figiel
@megapctr
Nov 07 2016 15:35
I don't
Miracle Ayodele
@iammiracle
Nov 07 2016 15:35
i print user
and it gave me none
Filip Figiel
@megapctr
Nov 07 2016 15:35
yeah but everything you pass to authenticate is correct
unless you're shadowing the name authenticate
Miracle Ayodele
@iammiracle
Nov 07 2016 15:36
yeah
so y is user not giving me wat i authenticated
its returning none
Filip Figiel
@megapctr
Nov 07 2016 15:39
are you shadowing the name authenticate somewhere?
perhaps you could paste entire views.py file
Miracle Ayodele
@iammiracle
Nov 07 2016 15:41
ok
one for registration

var code ="def Registration(request):

context = {}
form = RegistrationForm(data=request.POST)
if request.POST:
    if form.is_valid():
        new_member = form.save()
        # return render_to_response('registration/success.html',)
        return HttpResponseRedirect('/home/')
context.update({'form': form})
return render(request, 'index.html', context)

";

second for login
Filip Figiel
@megapctr
Nov 07 2016 15:42
no, I mean the entire views.py file
Miracle Ayodele
@iammiracle
Nov 07 2016 15:44

var code = "from django.shortcuts import render, render_to_response

from django.http import HttpResponseRedirect, HttpResponse
from index.form import *
from django.contrib.auth import authenticate, login
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext

def Registration(request):
context = {}
form = RegistrationForm(data=request.POST)
if request.POST:
if form.is_valid():
new_member = form.save()

        # return render_to_response('registration/success.html',)
        return HttpResponseRedirect('/home/')
context.update({'form': form})
return render(request, 'index.html', context)

def user_login(request):
context = {}

# print (request.user.is_authenticated())
if request.method == 'POST':
    print request.POST
    email = request.POST['email']
    password = request.POST['password']
    # print email
    # print password
    user = authenticate(email=email, password=password)
    print user
    if user:
        if user.is_active:
            login(request, user)
            print (request.user.is_authenticated())
            print user
            return HttpResponseRedirect('/home/')
        else:
            print (request.user.is_authenticated())
            print user
            return HttpResponse("Your Account is disabled")
    else:
        print (request.user.is_authenticated())
        print "Invalid login details: {0}, {1}".format(email, password)
        return HttpResponse("Invalid login details supplied")
else:
    print (request.user.is_authenticated())
    return render(request, 'index.html', context)

";

Filip Figiel
@megapctr
Nov 07 2016 15:45
looks fine
I'm really out of ideas
just to be sure, you can add print authenticate to see if it's really django's authenticate function
Miracle Ayodele
@iammiracle
Nov 07 2016 15:48
you mean print user
i even noticed the Registration function is not allowing the login function work
Filip Figiel
@megapctr
Nov 07 2016 15:49
how so?
Miracle Ayodele
@iammiracle
Nov 07 2016 15:49
i had to comment the Registration funtion
i dont even know what is wrong yet
i want to try fix the login first
Filip Figiel
@megapctr
Nov 07 2016 15:50
what happens if you don't comment it out?
Miracle Ayodele
@iammiracle
Nov 07 2016 15:50
it wont gv me anything
Filip Figiel
@megapctr
Nov 07 2016 15:50
if you uploaded your django project to GH, perhaps we could work on that
Miracle Ayodele
@iammiracle
Nov 07 2016 15:50
aiit
no problem
thanks for your time @megapctr
i read smtin related to AUTHENTICATION_BACKENDS trying to see how it can help tho
Filip Figiel
@megapctr
Nov 07 2016 15:53
correct, but if you didn't change it, it should work out of the box
Miracle Ayodele
@iammiracle
Nov 07 2016 15:56
ok