From 84e402bcaa8fe0fec16ffaa93f679706dbed0f08 Mon Sep 17 00:00:00 2001 From: Phoenix009 Date: Wed, 11 Nov 2020 16:21:21 +0530 Subject: [PATCH] authentication backend --- src/core/settings.py | 9 ++++++ src/core/templates/base.html | 3 ++ src/core/templates/partials/_alerts.html | 18 +++++++++++ src/core/templates/partials/_head.html | 4 ++- src/core/templates/partials/_topbar.html | 2 +- src/core/templates/user_base.html | 1 + src/core/urls.py | 5 ++- src/users/forms.py | 11 +++++++ src/users/templates/users/login.html | 40 ++++++++++-------------- src/users/templates/users/register.html | 38 +++++++++------------- src/users/urls.py | 15 +++++++++ src/users/views.py | 22 +++++++++++-- 12 files changed, 115 insertions(+), 53 deletions(-) create mode 100644 src/core/templates/partials/_alerts.html create mode 100644 src/users/forms.py create mode 100644 src/users/urls.py diff --git a/src/core/settings.py b/src/core/settings.py index 15f84cb..2d64a76 100644 --- a/src/core/settings.py +++ b/src/core/settings.py @@ -41,6 +41,8 @@ INSTALLED_APPS = [ 'classroom', 'posts', 'users', + 'crispy_forms', + 'widget_tweaks', ] MIDDLEWARE = [ @@ -133,3 +135,10 @@ STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'core/static'), ] + + +LOGIN_REDIRECT_URL = 'classroom:home' +LOGOUT_REDIRECT_URL = 'landing-page' + +CRISPY_TEMPLATE_PACK = 'bootstrap4' + diff --git a/src/core/templates/base.html b/src/core/templates/base.html index 34e5695..f32403f 100644 --- a/src/core/templates/base.html +++ b/src/core/templates/base.html @@ -17,10 +17,13 @@
+ {% include 'partials/_topbar.html' %}
+ {% include 'partials/_alerts.html' %} + {% block content %} {% endblock content %}
diff --git a/src/core/templates/partials/_alerts.html b/src/core/templates/partials/_alerts.html new file mode 100644 index 0000000..3092be4 --- /dev/null +++ b/src/core/templates/partials/_alerts.html @@ -0,0 +1,18 @@ +{% if messages %} + {% for message in messages %} +
+ +
+ {% endfor %} +{% endif %} + diff --git a/src/core/templates/partials/_head.html b/src/core/templates/partials/_head.html index 9b11293..a0bc219 100644 --- a/src/core/templates/partials/_head.html +++ b/src/core/templates/partials/_head.html @@ -1,3 +1,5 @@ +{% load static %} + @@ -27,4 +29,4 @@ - \ No newline at end of file + diff --git a/src/core/templates/partials/_topbar.html b/src/core/templates/partials/_topbar.html index 83838ce..8cdd277 100644 --- a/src/core/templates/partials/_topbar.html +++ b/src/core/templates/partials/_topbar.html @@ -179,4 +179,4 @@ - \ No newline at end of file + diff --git a/src/core/templates/user_base.html b/src/core/templates/user_base.html index 6492ad2..1235aa7 100644 --- a/src/core/templates/user_base.html +++ b/src/core/templates/user_base.html @@ -8,6 +8,7 @@
+ {% include 'partials/_alerts.html' %} {% block content %} {% endblock content %}
diff --git a/src/core/urls.py b/src/core/urls.py index 7cd66c8..1114900 100644 --- a/src/core/urls.py +++ b/src/core/urls.py @@ -17,6 +17,7 @@ from django.contrib import admin from django.urls import path,include from django.conf.urls.static import static from django.conf import settings +from django.contrib.auth.views import LoginView, LogoutView from . import views @@ -24,5 +25,7 @@ from . import views urlpatterns = [ path('admin/', admin.site.urls), path('classroom/',include('classroom.urls')), - path('', views.landing, name='langing-page'), + path('', views.landing, name='landing-page'), + path('users/', include('users.urls')), + ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/src/users/forms.py b/src/users/forms.py new file mode 100644 index 0000000..e1b93b5 --- /dev/null +++ b/src/users/forms.py @@ -0,0 +1,11 @@ +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User +from django import forms + +class UserRegistrationForm(UserCreationForm): + email = forms.EmailField() + + class Meta: + model = User + fields= ['username', 'email', 'password1', 'password2'] + diff --git a/src/users/templates/users/login.html b/src/users/templates/users/login.html index a9e5d91..58fa3ec 100644 --- a/src/users/templates/users/login.html +++ b/src/users/templates/users/login.html @@ -1,4 +1,8 @@ {% extends 'user_base.html' %} +{% load static %} +{% load crispy_forms_tags %} +{% load widget_tweaks %} + {% block title %} Forgot Password {% endblock title %} {% block content %} @@ -17,29 +21,17 @@

Welcome Back!

-
-
- -
-
- -
-
-
- - -
-
- - Login - -
- - Login with Google - - - Login with Facebook - + + {% csrf_token %} +
+ {{ form.username | attr:'class: form-control form-control-user'| attr:'placeholder: Username' }} +
+ +
+ {{ form.password | attr:'class: form-control form-control-user'| attr:'placeholder: Password' }} +
+ +

@@ -57,4 +49,4 @@
-{% endblock content %} \ No newline at end of file +{% endblock content %} diff --git a/src/users/templates/users/register.html b/src/users/templates/users/register.html index 3d2c19f..6f3bdda 100644 --- a/src/users/templates/users/register.html +++ b/src/users/templates/users/register.html @@ -1,5 +1,7 @@ {% extends 'user_base.html' %} -{% block title %} Forgot Password {% endblock title %} +{% load widget_tweaks %} + +{% block title %} Register {% endblock title %} {% block content %}
@@ -12,36 +14,24 @@

Create an Account!

-
-
-
- -
-
- -
+ + {% csrf_token %} + +
+ {{ form.username | attr:'class: form-control form-control-user'|attr:'placeholder:Username' }}
- + {{ form.email | attr:'class: form-control form-control-user'|attr:'placeholder:Email' }}
- + {{ form.password1 | attr:'class: form-control form-control-user'|attr:'placeholder:Password' }}
- + {{ form.password2 | attr:'class: form-control form-control-user'|attr:'placeholder:Confirm Password' }}
-
- - Register Account - -
- - Register with Google - - - Register with Facebook - +
+

@@ -55,4 +45,4 @@
-{% endblock content %} \ No newline at end of file +{% endblock content %} diff --git a/src/users/urls.py b/src/users/urls.py new file mode 100644 index 0000000..ee086f1 --- /dev/null +++ b/src/users/urls.py @@ -0,0 +1,15 @@ +from django.urls import path +from django.contrib.auth.views import LoginView, LogoutView +from django.conf import settings + +from . import views + +app_name= 'users' + +urlpatterns = [ + path('login/', LoginView.as_view(template_name="users/login.html"),name="login"), + path('logout/', LogoutView.as_view(), {'next_page': settings.LOGOUT_REDIRECT_URL},name="logout"), + path('register/', views.register, name='register') +] + + diff --git a/src/users/views.py b/src/users/views.py index 91ea44a..34fe6e7 100644 --- a/src/users/views.py +++ b/src/users/views.py @@ -1,3 +1,21 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect +from django.contrib import messages +from .forms import UserRegistrationForm + + +def register(request): + if request.method == 'POST': + form = UserRegistrationForm(request.POST) + if form.is_valid(): + form.save() + username = form.cleaned_data.get('username') + messages.success(request, f'Account Created Successfully for {username}') + return redirect('users:login') + else: + messages.success(request, f'Error Setting up the account') + else: + form = UserRegistrationForm() + return render(request, 'users/register.html', {'form':form}) + + -# Create your views here.