Django User Accounts Login, register and Logout User Authentication
#views.py from django.shortcuts import render, redirect from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import authenticate, login from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth import logout from django.shortcuts import HttpResponseRedirect def signup(request): if request.user.is_authenticated: return redirect('/') if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') user = authenticate(username=username, password=password) login(request, user) return redirect('/') else: return render(request, 'signup.html', {'form': form}) else: form = UserCreationForm() return render(request, 'signup.html', {'form': form}) def home(request): return render(request, 'home.html') def signin(request): if request.user.is_authenticated: return render(request, 'home.html') if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect('/profile') #profile else: msg = 'Error Login' form = AuthenticationForm(request.POST) return render(request, 'login.html', {'form': form, 'msg': msg}) else: form = AuthenticationForm() return render(request, 'login.html', {'form': form}) def profile(request): return render(request, 'profile.html') def signout(request): logout(request) return redirect('/')
#urls.py from django.contrib import admin from django.urls import path from myapp import views urlpatterns = [ path('admin/', admin.site.urls), path('',views.home), path('signin/',views.signin), path('signout/',views.signout), path('signup/',views.signup), path('profile/',views.profile), ]
//templates/home.html {% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Welcome!</h2> {% if user.is_authenticated %} Hi {{ user.username }}! <a href="/signout" class="btn btn-danger">Logout</a> {% else %} <a href="/signin" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span>Login</a> <a href="/signup" class="btn btn-success">Signup</a> {% endif %} {% endblock %}
//templates/base.html <!DOCTYPE html> <html lang="en"> <head> <title>{% block title %}Django User Accounts{% endblock %}</title> {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.css' %}"/> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> </head> <body> <div class="container"> <header> <h1>Django User Accounts</h1> </header> <hr> <main> {% block content %} {% endblock %} </main> <hr> </div> </body> </html>
//templates/signup.html {% extends 'base.html' %} {% block content %} <h2>Sign up</h2> <form method="post"> {% csrf_token %} {% for field in form %} <p> {{ field.label_tag }}<br> {{ field }} {% for error in field.errors %} <p style="color: red">{{ error }}</p> {% endfor %} </p> {% endfor %} <button type="submit" class="btn btn-success">Sign up</button> </form> {% endblock %}
//templates/login.html {% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Login</h2> {{ msg }} <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit" class="btn btn-primary">Login</button> </form> {% endblock %}
//templates/profile.html {% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Welcome!</h2> {% if user.is_authenticated %} Hi {{ user.username }}! <a href="/signout" class="btn btn-danger">Logout</a> {% else %} <a href="/signin" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span>Login</a> <a href="/signup" class="btn btn-success">Signup</a> {% endif %} {% endblock %}