article

Friday, April 24, 2020

Python Django - Simple Registration & Login Form


Python Django - Simple Registration & Login Form


 
#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)
 
#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"
 
#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),
]  
//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 %}
//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">
            <a class="navbar-brand" href="https://tutorial101.blogspot.com">Cairocoders</a>
        </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>
//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 %}

Related Post