Django authenticate method return None - python

I've read a lot topics about this problem but still can't fix problem.
I am going by Tango With Django tutorial (part 9) and get strange problem.
When I create user, I can't then get user object using authenticate method.
GitHub project: (link)
This is my register function from views:
def register(request):
# A boolean value for telling the template whether the registration was
# successful.
# Set to False initially. Code changes value to True when registration
# succeeds.
registered = False
# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
# Attempt to grab information from the raw form information
# Note that we make use of both UserForm and UserProfileForm
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
# If the two forms are valid...
if user_form.is_valid() and profile_form.is_valid():
# Save the user's form data to the database.
user = user_form.save()
# Now we hash the password with the set_password method.
# Once hashed, we can update the user object
user.set_password(user.set_password)
user.save()
# Now sort out the UserProfile instance.
# Since we need to set the user attribute ourselves, we set
# commit=False
# This delays saving the model until we're ready to avoid
# integrity problems.
profile = profile_form.save(commit=False)
profile.user = user
# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the
# UserProfile
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
# Now we save the UserProfile model instance
profile.save()
# Update our variable to tell the template registration was
# successful
registered = True
# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also shown to the user
else:
print user_form.errors, profile_form.errors
# Not a HTTP POST, so we render out form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
user_form = UserForm()
profile_form = UserProfileForm()
context_dict = {
'user_form': user_form,
'profile_form': profile_form,
'registered': registered
}
# Render the template depending on the context.
return render(request, 'rango/register.html', context_dict)
user_login function from views:
def user_login(request):
# If the request is a HTTP POST, try to pull out the relevant information
if request.method == 'POST':
# Gather the username and password provided by the user.
# This information is obtained from the login form.
# We use request.POST.get('<variable>') as opposed to
# request.POST['variable'], because the
# request.POST.get('<variables>') return None, if the value does not
# exist, while the request.POST['<variable>'] will raise
# key error exception
username = request.POST.get('username')
password = request.POST.get('password')
# Use Django's machinery to attempt to see if the username/password
# combination is valid - a User object is returned if it is.
user = authenticate(username=username, password=password)
# user = User.objects.get(username='user1')
print user
# If we have a User object, the details are correct.
# If None (Python's way of representing the absence of a value), no
# user with matching credentials was found.
if user:
# Is the account active? It could have been disabled
if user.is_active:
# If the account is valid and active, we can log the user in.
# We'll send the user back to the homepage
login(request, user)
return redirect('rango:index')
else:
# An inactive account was user - no logging in
return HttpResponse("Your Rango account is disabled.")
else:
# Bad login details were provided. So we can't log the user in.
print "Invalid login details: {0}, {1}".format(username, password)
return HttpResponse("Invalid login details supplied")
# The request is not a HTTP POST, so display the login form.
# This scenario would most likely be a HTTP GET.
else:
# No context variables to pass to the template system, hence
# the blank dictionary object..
return render(request, 'rango/login.html')

This is the code in Tango with django
user.set_password(user.password)
user.save()
And your's is
user.set_password(user.set_password)
user.save()
Try to replace set_password with password.

Related

Is there any Solution for this Login syntax>?

Ive this Django Login and Registration form
but the registration form is fetching in database auth_user but not in helloworld_register
This is my Registration code
def Register(request):
if request.method =='POST':
username=request.POST['username']
email=request.POST['email']
first_name=request.POST['first_name']
last_name=request.POST['last_name']
password1=request.POST['password1']
password2=request.POST['password2']
if User.objects.filter(email=email).exists():
messages.info(request, 'uh oh..:( This Email is Already Taken ')
print('emailtaken')
return redirect('/Register')
elif User.objects.filter(first_name=first_name).exists():
messages.info(request, 'uh oh..:( This Name is Already Taken ')
print('Name taken')
return redirect('/Register')
user=User.objects.create_user(username=username, email=email,first_name=first_name,last_name=last_name,password=password1)
user.save();
messages.info(request, 'Registration complete Login to continue ..:)')
print('user created')
return redirect('/LOGIN')
return render(request, 'Register.html')
And this is my Login Code
def LOGIN(request):
if request.method=='POST':
email=request.POST['email']
password1=request.POST['password1']
user=auth.authenticate(email=email,password1=password1)
#user.save();
if user is not None:
auth.LOGIN(request,user)
return redirect('LOGIN')
else:
messages.info(request, 'mmm :( Invalid Credentials ')
return redirect('LOGIN')
Even though if i try Logging in with registered credentilals im unable to login
First of all, I would like to correct the terminology. Both snippets you provided are not Forms but Views. And 'auth_user' is not a database, its a table, as I will assume for 'helloworld_register'.
Related to your first problem, it seems that you are using Django's default User model. And by default this model uses 'auth_user' both to create and retrieve objects.
If you want to change defaults, you are going to need a custom user model, in fact here is a quote from Django documentation about such:
If you’re starting a new project, it’s highly recommended to set up a
custom user model, even if the default User model is sufficient for
you. This model behaves identically to the default user model, but
you’ll be able to customize it in the future if the need arises
As for your second issue, take a look at this example. I see a few errors in your snippet, such as:
user=auth.authenticate(email=email,password1=password1)
# there is no field password1 in the User model.
if user is not None:
auth.LOGIN(request,user) # Wrong call do login() method
return redirect('LOGIN') # You want to redirect to some other place
Therefore you are probably looking for something like this:
(Also, do not ignore good practices such as views names in snake_case)
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect
def my_view(request):
email= request.POST['email']
password = request.POST['password1']
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
redirect('/your/app/index')
else:
messages.error(request, 'mmm: Invalid Credentials.')
redirect('/back/to/login')

Login page is verifying all users as good Django

I'm trying to build a login page in Django. But whenever I try to authenticate the user it doesn't work. The user passes each time even if the user doesn't exist. Any help I would really appreciate it!
def login(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
form = CreateUserForm()
return render(request,
"leadfinderapp/login.html",
context={"form":form})
The user isn't actually getting authenticated, you just aren't handling errors in the form. The function is just rendering the "leadfinderapp/login.html" template again regardless of the user's data entered.
To handle errors in the form, you need to have a
if form.is_valid():
# submit information and login
else:
# re render form with errors
You should look into using Django All Auth to handle your authentication (https://django-allauth.readthedocs.io/en/latest/installation.html) it's customizable but handles a lot of the work for you - just like Django itself!

Force password reset on manually created User

In my project I have an open signup form, where you can create your Company and all the information bellow it.
After that you can invite people to help you administrate the information of your company. To do that, my idea was to, when the logged user add another admin, I would create the user manually with a fake password and send a Reset Password request to the created email, so he can create his own password. The important code is below:
from django.contrib.auth.forms import PasswordResetForm
...
def create_admin(request):
if request.method == 'POST':
form = AdminForm(request.POST)
if form.is_valid():
email = form.cleaned_data.get("email")
random_pass = User.objects.make_random_password()
user = User(username=email, email=email, password=random_pass)
user.save()
company.add_admin(user)
reset_form = PasswordResetForm({'email': email})
reset_form.save(
email_template_name="rh/password_reset_email.html",
subject_template_name="rh/password_reset_subject.txt")
return redirect('dashboard')
else:
form = AdminForm()
return render(request, 'rh/create_admin.html', {'form': form})
Unfortunately, the above code returns a Exception Type: AttributeError 'PasswordResetForm' object has no attribute 'cleaned_data'
To note:
I already have a fully working reset password feature, using everything from django and custom templates. That's why I'm trying to make this work this way
I would like to customize the email_template_name and subject_template_name, like in my code
thanks in advance
After a bit of dialog in comments I'll leave the response. The two issues were the way the password was being created and the form not being validated. This code should work:
email = form.cleaned_data.get("email")
random_pass = User.objects.make_random_password()
user = User(username=email, email=email)
user.set_password(random_pass)
user.save()
company.add_admin(user)
reset_form = PasswordResetForm({'email': email})
reset_form.is_valid()
reset_form.save(
email_template_name="rh/password_reset_email.html",
subject_template_name="rh/password_reset_subject.txt")
return redirect('dashboard')
(Note that in this code I used the form and not the view, because I'm not sure about what you did with that. If this code doesn't work please correct it.)

django cookies not getting set

i am trying to set django test cookies on homepage but it is not getting set set.I have tried everything from changing middleware classes to sessionengines
my index function
def index(request):
request.session.set_test_cookie()
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
here is my other function to test cookie
def register(request):
if request.session.test_cookie_worked():
print ">>>> TEST COOKIE WORKED!"
request.session.delete_test_cookie()
# Like before, get the request's context.
context = RequestContext(request)
# A boolean value for telling the template whether the registration was successful.
# Set to False initially. Code changes value to True when registration succeeds.
registered = False
# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
# Attempt to grab information from the raw form information.
# Note that we make use of both UserForm and UserProfileForm.
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
# If the two forms are valid...
if user_form.is_valid() and profile_form.is_valid():
# Save the user's form data to the database.
user = user_form.save()
# Now we hash the password with the set_password method.
# Once hashed, we can update the user object.
user.set_password(user.password)
user.save()
# Now sort out the UserProfile instance.
# Since we need to set the user attribute ourselves, we set commit=False.
# This delays saving the model until we're ready to avoid integrity problems.
profile = profile_form.save(commit=False)
profile.user = user
# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the UserProfile model.
# Now we save the UserProfile model instance.
profile.save()
# Update our variable to tell the template registration was successful.
registered = True
# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print user_form.errors, profile_form.errors
# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
user_form = UserForm()
profile_form = UserProfileForm()
# Render the template depending on the context.
return render_to_response(
'register.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
context)
you do not need to do all those middle ware changes to your settings file. I have just tried setting up a simple demonstration of your code and it works perfect.
First thing, try minimizing your code to only the cookie setting 'experiment'. You can try this demo here.
One one view maybe an index of a home view set the cookie.
On another view maybe an index view of another app test the cookie.
From the docs you can only test using another page request I quote
*test_cookie_worked()* "Returns either True or False, depending on whether the user’s
browser accepted the test cookie. Due to the way cookies work,
you’ll have to call set_test_cookie() on a previous,
separate page request. See Setting test cookies below for more information."
project/appxx/views ... request.session.set_test_cookie() #inside a view
project/appyy/views ... if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponse(">>>> TEST COOKIE WORKED!")
Then you can add on the logic later. I have used the HttResponse method instead of the print statment you used.

Django login(request, user) don't put user in session?

I can't seem to work out how to log in users in Django. I'm confused because the documentation tells you explicitely how to do it, but still somehow I must be making a mistake.
The link
https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.login
says
"To log a user in, from a view, use login(). It takes an HttpRequest object and a User object. login() saves the user’s ID in the session, using Django’s session framework."
So I have the following views.py:
def login_view(request):
if request.method == 'GET':
return render(request, 'app/login.htm')
if request.method == 'POST':
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is None:
return HttpResponseRedirect(reverse('error'))
if not user.is_active:
return HttpResponseRedirect(reverse('error'))
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to a success page.
return HttpResponseRedirect(reverse('home'))
def home(request):
contextdict = {}
if request.session.user.is_authenticated():
contextdict['username'] = request.session.user.username
context = RequestContext(request, contextdict )
return render(request, 'app/home.htm', context)
Now, by using print 'qqq' I know for a fact that 'is None' and 'not is_active' have been evaluated to True, and so auth.login is evaluated and the HttpResponseRedirect is returned. I expected everythin to go normally and the user to be logged in, and the user name to be passed as context in the home view. However, Django gives me the following error:
AttributeError at /app/home/
'SessionStore' object has no attribute 'user'
Yeah, I have no idea what I'm doing.
You should use request.user to get user object, not request.session.user.
The data in session is used to retrieve the user object, but the session does not contain the actual user

Categories

Resources