Pages

Creating users (login and logout pages)

*Create new project using command

django-admin startproject userproject

*Make migrations by running following commands,

python3 manage.py makemigrations

python3 manage.py migrate

* create an app name "home"

python3 manage.py startapp home

*Register your app by copying the app name from "app.py" and put it in "settings.py" of userproject.

# websites which will use this projects
ALLOWED_HOSTS = ['machlearn1.blogspot.com']

INSTALLED_APPS = [
'home.apps.HomeConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

Create static and templates folder. Set static dir from site

STATIC_URL = '/static/'
# pasted from django website
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]

Set dir for templates too,

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Create an index.html file as a template.

In urls.py file of userproject, 

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
]

Create urls.py file in the home folder,

from django.contrib import admin
from django.urls import path, include
from home import views

urlpatterns = [
path('', views.index, name='home'),
path('login', views.loginuser, name='login'),
path('logout', views.logoutuser, name='logout'),
]

create super user using command, "python3 manage.py createsuperuser".

Now create index.html, login.html and logout.html files. views.py file of home will be as below,

index.html file

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Logged in User</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
Welcome {{ request.user }}
<p>
This is the best website in the world.
</p>
<a href='/logout' > Logout </a>

</body>
</html>

login.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.88.1">
<title>Login</title>

<link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/sign-in/">


<!-- Bootstrap core CSS -->
<link href="/docs/5.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/5.1/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/5.1/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/5.1/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon.ico">
<meta name="theme-color" content="#7952b3">


<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}

@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>

<!-- Custom styles for this template -->
<link href="signin.css" rel="stylesheet">
</head>
<body class="text-center">

<main class="form-signin">
<form method='post' action='/login'>
{% csrf_token %}
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>

<div class="form-floating">
<input type="text" class="form-control" name='username'
            id="floatingInput" placeholder="name">
<label for="floatingInput">User name</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" name='password'
            id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
</div>

<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
</form>
</main>


</body>
</html>

views.py file

from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth import logout, authenticate, login

# password for test user divyababy

# Create your views here.
def index(request):
if request.user.is_anonymous:
return redirect('/login')
return render(request,'index.html')

def loginuser(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')

# checking for user details whether details are correct
user = authenticate(username=username,password=password)

# details are correct redirect to home page
if user is not None:
login(request,user)
return redirect('/')
else:
return render(request,'login.html')

return render(request, 'login.html')

def logoutuser(request):
logout(request)
return redirect('/login')


No comments:

Post a Comment

If you have any doubt, let me know

Email Subscription

Enter your email address:

Delivered by FeedBurner

INSTAGRAM FEED