Online voting system
I'm trying to change the login/signup experience of my django project. To do so, I did the following:
- I created a dedicated app
mysuermodelthat I using to handling authentication et cetera. - I changed the user_mode to remove username as unique id and put email address.
- I created a dedicated
form,viewandhtmlto allow users to sign-in and/or sign-up.
However, i think I have been messy in my doing since a few things do not add up.
Most of all, I can't find a way to remove the view that is currently found at http://127.0.0.1:8000/login/ (the default django login view).
I tried the following solution but when I hit /login I still land on the default view. Any help?
main app url file
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('action.urls')),
path('r/', include('urlshortener.urls')),
path('myusermodel/',include('myusermodel.urls')),
path('login/',RedirectView.as_view(url='myusermodel', permanent=False), name='index'),
path('accounts/', include('allauth.urls')),
myusermodel url file
from django.urls import path
from myusermodel.views import sign_up
from myusermodel.views import sign_in
app_name = 'myusermodel'
urlpatterns = [
# login
path('signup/', sign_up.sign_up, name='sign_up'),
path('signin/', sign_in.sign_in, name='sign_in'),
]
sign in view
from django.shortcuts import render,redirect
from myusermodel.forms.SignInForm import SignInForm
from django.contrib.auth import authenticate, login
from myusermodel.models import CustomUser
def sign_in(request):
form = SignInForm()
context = {
'form': form
}
if request.user.is_authenticated:
return redirect('action:home')
if request.method == 'POST':
form = SignInForm(data=request.POST)
if form.is_valid():
if CustomUser.objects.filter(email=form.cleaned_data.get('email')).exists():
email = form.cleaned_data.get('email')
password = form.cleaned_data.get('password')
user = authenticate(email=email,password=password)
if user is not None:
login(request,new_user)
return redirect('action:home')
else:
context['password_error'] = True
print('password_error')
else:
context['user_error'] = form.cleaned_data.get('email')
print('user_error')
return render(request, 'myusermodel/sign_in.html', context)