Python Django - Simple Registration & Login Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #view.py from django.shortcuts import render, redirect, HttpResponseRedirect from myapp.models import Member #models.py # Create your views here. def index(request): if request.method = = 'POST' : member = Member(username = request.POST[ 'username' ], password = request.POST[ 'password' ], firstname = request.POST[ 'firstname' ], lastname = request.POST[ 'lastname' ]) member.save() return redirect( '/' ) else : return render(request, 'index.html' ) def login(request): return render(request, 'login.html' ) def home(request): if request.method = = 'POST' : if Member.objects. filter (username = request.POST[ 'username' ], password = request.POST[ 'password' ]).exists(): member = Member.objects.get(username = request.POST[ 'username' ], password = request.POST[ 'password' ]) return render(request, 'home.html' , { 'member' : member}) else : context = { 'msg' : 'Invalid username or password' } return render(request, 'login.html' , context) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #models.py from django.db import models # Create your models here. class Member(models.Model): firstname = models.CharField(max_length = 30 ) lastname = models.CharField(max_length = 30 ) username = models.CharField(max_length = 30 ) password = models.CharField(max_length = 12 ) def __str__( self ): return self .firstname + " " + self .lastname class Meta: db_table = "web_member" |
1 2 3 4 5 6 7 8 9 10 11 | #urls.py from django.contrib import admin from django.urls import path from myapp import views urlpatterns = [ path( 'admin/' , admin.site.urls), path('',views.index), path( 'login/' ,views.login), path( 'home/' ,views.home), ] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //templates/index.html {% extends 'base.html' %} {% block body %} <form method= "POST" > {% csrf_token %} <div class = "col-md-2" > <a href= "/login" >Login</a> </div> <div class = "col-md-8" > <div class = "form-group" > <label for = "username" >Username</label> <input type= "text" name= "username" class = "form-control" required= "required" > </div> <div class = "form-group" > <label for = "password" >Password</label> <input type= "password" name= "password" class = "form-control" required= "required" /> </div> <div class = "form-group" > <label for = "firstname" >Firstname</label> <input type= "text" name= "firstname" class = "form-control" required= "required" /> </div> <div class = "form-group" > <label for = "lastname" >Lastname</label> <input type= "text" name= "lastname" class = "form-control" required= "required" /> </div> <br /> <div class = "form-group" > <button type= "submit" class = "btn btn-primary form-control" ><span class = "glyphicon glyphicon-save" ></span> Submit</button> </div> </div> </form> {% endblock %} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //templates/base.html <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" name= "viewport" content= "width=device-width, initial-scale=1" /> <title>Python Django - Simple Registration & Login Form</title> {% load static %} <link rel= "stylesheet" type= "text/css" href= "{% static 'css/bootstrap.css' %}" /> </head> <body> <nav class = "navbar navbar-default" > <div class = "container-fluid" > </div> </nav> <div class = "col-md-3" ></div> <div class = "col-md-6 well" > <h3 class = "text-primary" >Python Django - Simple Registration & Login Form</h3> <hr style= "border-top:1px dotted #000;" /> {% block body %} {% endblock %} </div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //templates/login.html {% extends 'base.html' %} {% block body %} <form method= "POST" class = "post-form" action= "/home/" > {% csrf_token %} <div class = "col-md-2" > <a href= "/" >Signup</a> </div> <div class = "col-md-8" > <div class = "form-group" > <label for = "username" >Username</label> <input type= "text" name= "username" class = "form-control" required= "required" /> </div> <div class = "form-group" > <label for = "password" >Password</label> <input type= "password" name= "password" class = "form-control" required= "required" /> </div> <center><label class = "text-danger" >{{ msg }}</label></center> <br /> <div class = "form-group" > <button class = "btn btn-primary form-control" ><span class = "glyphicon glyphicon-log-in" ></span> Login</button> </div> </div> </form> {% endblock %} |