Here you will create a home page for your website. Further, you will learn how you can restrict it to be accessed only by logged in users and not anyone just by visiting the url.
Lets start by creating a home.html
file inside the templates folder.
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Authentication Demo</title>
</head>
<body>
<h1>Home Page</h1>
<h2>Restricted content to be accessed by only authenticated users</h2>
</body>
</html>
Next you need to create a function to render
this template
and route it using the urls.py
file.
First open the authentication/views.py
file and add the code to render this template.
def home(request):
return render(request, 'home.html')
This will render the home.html
file when the home()
function is called.
A route needs to be created inside the authentication/urls.py
file as follows :
from . import views
urlpatterns = [
path("", views.home, name='home'),
]
After this when you run python manage.py runserver
, you will be able to see a page like this.
python manage.py runserver
For now anyone can access this web page by visiting the url http://127.0.0.1:8000/, we will further restrict the access only to authenticated users.