diff --git a/FinTech_app/forms.py b/FinTech_app/forms.py new file mode 100644 index 0000000..9e18ec3 --- /dev/null +++ b/FinTech_app/forms.py @@ -0,0 +1,18 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + + +class NewUserForm(UserCreationForm): + email = forms.EmailField(required=True) + + class Meta: + model = User + fields = ("username", "email", "password1", "password2") + + def save(self, commit=True): + user = super(NewUserForm, self).save(commit=False) + user.email = self.cleaned_data['email'] + if commit: + user.save() + return user diff --git a/FinTech_app/settings.py b/FinTech_app/settings.py index b36822e..9ce0e62 100644 --- a/FinTech_app/settings.py +++ b/FinTech_app/settings.py @@ -37,6 +37,9 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'bootstrap4', + 'crispy_forms' + ] MIDDLEWARE = [ @@ -49,6 +52,8 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] +CRISPY_TEMPLATE_PACK = 'bootstrap4' + ROOT_URLCONF = 'FinTech_app.urls' TEMPLATES = [ @@ -116,11 +121,12 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.1/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = '/static/' # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -LOGIN_REDIRECT_URL = "/" \ No newline at end of file +LOGIN_REDIRECT_URL = "/" +LOGOUT_REDIRECT_URL = "/" diff --git a/FinTech_app/urls.py b/FinTech_app/urls.py index 3e2f55c..3ef3bb7 100644 --- a/FinTech_app/urls.py +++ b/FinTech_app/urls.py @@ -15,8 +15,14 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from django.views.generic.base import TemplateView +from . import views urlpatterns = [ path('admin/', admin.site.urls), - path("accounts/", include("django.contrib.auth.urls")), + path('', TemplateView.as_view(template_name='main.html'), name='home'), + path("register/", views.register_request, name="register"), + path("login/", views.login_request, name="login"), + path("logout/", views.logout_request, name="logout"), + path("main/", views.logout_request, name="main"), ] diff --git a/FinTech_app/views.py b/FinTech_app/views.py new file mode 100644 index 0000000..3cff995 --- /dev/null +++ b/FinTech_app/views.py @@ -0,0 +1,50 @@ +from django.shortcuts import render, redirect +from .forms import NewUserForm +from django.contrib.auth import login, authenticate, logout +from django.contrib import messages +from django.contrib.auth.forms import AuthenticationForm + + +def register_request(request): + if request.method == "POST": + form = NewUserForm(request.POST) + if form.is_valid(): + user = form.save() + login(request, user) + messages.success(request, "Registration successful." ) + return redirect("/") + messages.error(request, "Unsuccessful registration. Invalid information.") + form = NewUserForm() + return render(request=request, template_name="registration/register.html", context={"register_form":form}) + + +def login_request(request): + if request.method == "POST": + form = AuthenticationForm(request, data=request.POST) + if form.is_valid(): + username = form.cleaned_data.get('username') + password = form.cleaned_data.get('password') + user = authenticate(username=username, password=password) + if user is not None: + login(request, user) + messages.info(request, f"You are now logged in as {username}.") + return redirect("/") + else: + messages.error(request, "Invalid username or password.") + else: + messages.error(request, "Invalid username or password.") + form = AuthenticationForm() + return render(request=request, template_name="registration/login.html", context={"login_form":form}) + + +def logout_request(request): + logout(request) + messages.info(request, "You have successfully logged out.") + return redirect("/") + + +def index(request): + if request.user.is_authenticated: + print("Logged in") + else: + print("Not logged in") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bc15e94..4abb1e3 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/static/main.css b/static/main.css new file mode 100644 index 0000000..830880c --- /dev/null +++ b/static/main.css @@ -0,0 +1,3 @@ +.grad { + background-image: linear-gradient(to right, orange , white); +} \ No newline at end of file diff --git a/static/registration/login.css b/static/registration/login.css new file mode 100644 index 0000000..f9d1374 --- /dev/null +++ b/static/registration/login.css @@ -0,0 +1,90 @@ +/* 'Open Sans' font from Google Fonts */ +@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700); + +body { + background: #456; + font-family: 'Open Sans', sans-serif; +} + +.login { + width: 400px; + margin: 16px auto; + font-size: 16px; +} + +/* Reset top and bottom margins from certain elements */ +.login-header, +.login p { + margin-top: 0; + margin-bottom: 0; +} + +/* The triangle form is achieved by a CSS hack */ +.login-triangle { + width: 0; + margin-right: auto; + margin-left: auto; + border: 12px solid transparent; + border-bottom-color: #28d; +} + +.login-header { + background: #28d; + padding: 20px; + font-size: 1.4em; + font-weight: normal; + text-align: center; + text-transform: uppercase; + color: #fff; +} + +.login-container { + background: #ebebeb; + padding: 12px; +} + +/* Every row inside .login-container is defined with p tags */ +.login p { + padding: 12px; +} + +.login input { + box-sizing: border-box; + display: block; + width: 100%; + border-width: 1px; + border-style: solid; + padding: 16px; + outline: 0; + font-family: inherit; + font-size: 0.95em; +} + +.login input[type="email"], +.login input[type="password"] { + background: #fff; + border-color: #bbb; + color: #555; +} + +/* Text fields' focus effect */ +.login input[type="email"]:focus, +.login input[type="password"]:focus { + border-color: #888; +} + +.login input[type="submit"] { + background: #28d; + border-color: transparent; + color: #fff; + cursor: pointer; +} + +.login input[type="submit"]:hover { + background: #17c; +} + +/* Buttons' focus effect */ +.login input[type="submit"]:focus { + border-color: #05a; +} \ No newline at end of file diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..62e74ca --- /dev/null +++ b/templates/main.html @@ -0,0 +1,70 @@ +{% load static %} + + +
+ + + + + + + +Nie masz konta? Zarejestruj się.
+If you already have an account, login instead.
+