26
loading...
This website collects cookies to deliver better user experience
level
that determines its priority (e.g., info
, warning
, or error
).include
tag. include
tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.partials
inside the templates
folder and within the partials
folder, create a alerts.html
file and add the following code:{% if messages %}
{% for message in messages %}
{% if message.tags == 'error' %}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{message}}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% else %}
<div class="alert alert-{{message.tags}} alert-dismissible fade show" role="alert">
{{message}}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
{% endfor %}
{% endif %}
messages
sequence, because otherwise the message storage will not be cleared for the next request. Note that we are using an if condition to check if the message.tag
is error, then we are showing danger alert, else we are directly showing the alerts using the message.tag
. This is because, in Bootstrap we have danger class, but Iin Django we have error tag. accounts/views.py
file and add the following code:from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.models import User
def signup(request):
if request.user.is_authenticated:
return redirect('index')
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
try:
User.objects.get(username=username)
messages.error(request, 'User already exists')
except User.DoesNotExist:
User.objects.create_user(username=username, password=password)
messages.success(request, 'Signup success')
return redirect('signin')
messages.error(request, "Username or Password is missing!")
return render(request, 'signup.html')
accounts/urls.py
file:from django.urls import path
from accounts.views import signup
urlpatterns = [
path('signup', signup, name='signup'),
]
templates/signup.html
, and add the following code:{% extends "base.html" %}{% load static %} {% block title %} Sign Up {% endblock title %}
{% block custom_head %}
<link rel="stylesheet" href="{% static 'css/signin.css' %}" />
{% endblock custom_head %}
{% block content %}
<body class="text-center">
<main class="form-signin">
<form method="POST" action="{% url 'signup' %}">
{% csrf_token %}
<h1 class="h3 mb-3 fw-normal">Please sign up</h1>
{% include 'partials/alerts.html' %}
<div class="form-floating mt-2">
<input
type="text"
class="form-control"
id="floatingInput"
name="username"
placeholder="johndoe"
/>
<label for="floatingInput">Username</label>
</div>
<div class="form-floating mt-2">
<input
type="password"
class="form-control"
id="floatingPassword"
name="password"
placeholder="Password"
/>
<label for="floatingPassword">Password</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">
Sign up
</button>
<p class="mt-3">
Already Registered? <a href="{% url 'signin' %}">Sign in now</a>
</p>
</form>
</main>
</body>
{% endblock content %}
POST
and the action URL is the signup route we just created. Also we are adding the URL of the sign in page in the Already Registered? line. We haven't created this route yet. from django.contrib.auth import authenticate, login
def signin(request):
if request.user.is_authenticated:
return redirect('index')
redirect_url = request.GET.get('next')
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
user = authenticate(username=username, password=password)
if user:
login(request, user)
if redirect_url:
return redirect(redirect_url)
return redirect('index')
else:
messages.error(request, 'Incorrect username or password!')
return redirect(f'/accounts/signin?next={redirect_url}')
messages.error(request, "Username or Password is missing!")
return render(request, 'signin.html')
accounts/urls.py
file:from django.urls import path
from accounts.views import signin, signup
urlpatterns = [
path('signup', signup, name='signup'),
path('signin', signin, name='signin'),
]
signin.html
file:{% extends "base.html" %}{% load static %} {% block title %} Sign In {% endblock title %}
{% block custom_head %}
<link rel="stylesheet" href="{% static 'css/signin.css' %}" />
{% endblock custom_head %}
{% block content %}
<body class="text-center">
<main class="form-signin">
<form method="POST" action="{% url 'signin' %}{% if request.GET.next %}?next={{request.GET.next}}{% endif %}">
{% csrf_token %}
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
{% include 'partials/alerts.html' %}
<div class="form-floating mt-2">
<input
type="text"
class="form-control"
id="floatingInput"
name="username"
placeholder="johndoe"
/>
<label for="floatingInput">Username</label>
</div>
<div class="form-floating mt-2">
<input
type="password"
class="form-control"
id="floatingPassword"
name="password"
placeholder="Password"
/>
<label for="floatingPassword">Password</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">
Sign in
</button>
<p class="mt-3">New user? <a href="{% url 'signup' %}">Sign up now</a></p>
</form>
</main>
</body>
{% endblock content %}
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
@login_required
def signout(request):
logout(request)
messages.info(request, "Logged out!")
return redirect('signin')
@login_required
decorator so that only authenticated users can logout. Let's add this view function in the urls.from django.urls import path
from accounts.views import signin, signout, signup
urlpatterns = [
path('signup', signup, name='signup'),
path('signin', signin, name='signin'),
path('signout', signout, name='signout'),
]
settings.py
file:LOGIN_URL = '/accounts/signin'
index.html
file and add:{% extends "base.html" %}{% load static %} {% block title %}View Bag{% endblock title %}
{% block content %}
<body>
<div class="container mt-5">
<!-- top -->
<div class="row">
<div class="col-lg-6">
<h1>View Grocery List</h1>
</div>
<div class="col-lg-6 float-right">
<div class="row">
<div class="col-lg-6">
<!-- Date Filtering-->
<input type="date" class="form-control" />
</div>
<div class="col-lg-4">
<input type="submit" class="btn btn-danger" value="filter" />
</div>
<div class="col-lg-2">
<p class="mt-1"><a href="{% url 'signout' %}">Log Out</a></p>
</div>
</div>
</div>
</div>
<!-- // top -->
<!-- Grocery Cards -->
<div class="row mt-4">
<!--- -->
<!-- Loop This -->
<div class="col-lg-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Tomato</h5>
<h6 class="card-subtitle mb-2 text-muted">2 Pcs.</h6>
<p class="text-success">BOUGHT</p>
</div>
</div>
</div>
<!-- // Loop -->
<div class="col-lg-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Chicken</h5>
<h6 class="card-subtitle mb-2 text-muted">2Kgs</h6>
<p class="text-danger">NOT AVAILABLE</p>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Posto</h5>
<h6 class="card-subtitle mb-2 text-muted">50gms</h6>
<p class="text-info">PENDING</p>
</div>
</div>
</div>
</div>
</div>
</body>
{% endblock content %}
LOGIN_URL
.from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def index(request):
return render(request, "index.html")
$ python manage.py makemigrations
$ python manage.py migrate