31
loading...
This website collects cookies to deliver better user experience
Django: The web framework for perfectionists with deadlines
pip install django
django-admin startproject user_management
startproject
command created for us. But be free to ask me what you don't understand in the comments section below, and I'll try my best to answer them.python manage.py startapp users
INSTALLED_APPS = [
# list of other apps django created for us.
'users' # add this
]
from django.shortcuts import render
def home(request):
return render(request, 'users/home.html')
from django.urls import path
from .views import home
urlpatterns = [
path('', home, name='users-home'),
]
Since we used our home view to handle the logic for the route we created, we should import our home view from views.py
I prefixed the name of the path with 'users' to avoid any collides with other app routes. I know we don't have any other app right now but it's a good practice.
include()
function from django.urls
. from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('users.urls')), # This is what we added
]
path('', include('users.urls'))
-> I put the empty string because I want the users app home page to be the home page of the entire website(http://localhost:8000/). <!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>{% block title %} {% endblock %} </title>
</head>
<body>
<div class="container p-3 my-3">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-expand-md navbar-light " style="background-color: #f0f5f5">
<a href="/" class="navbar-brand">Home</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav ml-auto">
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Logout</a>
<a href="#" class="nav-item nav-link">Sign in</a>
</div>
</div>
</nav>
{% block content %}{% endblock %}
</div>
</div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
What we did is setup the basic html structure and I used Bootstrap to give some styling and responsiveness to our pages. Note that all the links inside the nav
are dead links since we haven't created any other pages yet.
In the future, We will also add conditions to display login/logout buttons depending on whether the user is authenticated or not.
All the other html pages will extend from the above base html, while at the same time adding their own unique title, and content. Feel free to play with it and make it look more awesome for your needs.
{% extends "users/base.html" %}
{% block title %} Home Page {% endblock title%}
{% block content %}
<div class="jumbotron">
<h1 class="display-4">Welcome</h1>
<p class="lead">
This is <b>user registration and login system</b> build with django which is
a Python-based free and open-source web framework that follows the model–template–views architectural pattern.
</p>
<hr class="my-4">
<p class="lead">
<a class="btn btn-primary btn-lg" href="#" role="button">Logout</a>
<a class="btn btn-primary btn-lg" href="#" role="button">Sign in</a>
</p>
</div>
{% endblock content %}
python manage.py runserver
and go to http://localhost:8000/