I have created an App called "myapp" with custom user called "CustomUser" along with a custom user app called "customuser". I am able to successfully login from the admin. But I am not able to login from the app login.
Here is the login function:
def login(request):
if request.method == 'POST':
email=request.POST.get('email')
password=request.POST.get("password")
user = authenticate(request,email=email,password=password)
if user is not None:
auth_login(request,user)
messages.success(request,'You are logged in')
else:
messages.error(request,"invalid login credentials")
return redirect(login)
return redirect(request,'myapp/home.html')
else:
form = AuthenticationForm()
return render(request,'customuser/login.html', {'form':form})
Here is the admin:
from customuser.forms import *
from customuser.models import Profiles
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
filter_horizontal=()
list_display = ('email', 'FirstName','LastName', 'last_login','is_active','date_joined','is_admin')
list_filter = ('is_admin','is_staff')
fieldsets = (
(None, {'description': (
"Enter the new user's name and email address and click save."
" The user will be emailed a link allowing them to login to"
" the site and set their password."
),
'fields': ('email', )}),
('Password', {
'description': "Optionally, you may set the user's password here.",
'fields': ('password',),
'classes': ('collapse', 'collapse-closed'),
}),
('Personal info', {'fields': ('FirstName','LastName')}),
('Permissions', {'fields': ('is_admin','is_staff')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password', 'FirstName','LastName', 'is_admin'),
}),
)
search_fields = ('email','FirstName','LastName')
ordering = ('email','FirstName','LastName')
filter_horizontal = ()
def save_model(self, request, obj, form, change):
if not change and (not form.cleaned_data['password'] or not obj.has_usable_password()):
obj.set_password(get_random_string())
reset_password = True
else:
reset_password = False
super(UserAdmin, self).save_model(request, obj, form, change)
if reset_password:
reset_form = PasswordResetForm({'email': obj.email})
assert reset_form.is_valid()
reset_form.save(
request=request,
use_https=request.is_secure(),
subject_template_name='registration/account_creation_subject.txt',
email_template_name='registration/account_creation_email.html',
)
admin.site.register(CustomUser, UserAdmin)
admin.site.unregister(Group)
It looks like the login function is not resulting in user object.
Solution was creating a new form that doesn't inherit from base Django AuthenticationForm and not using ModelForm.