Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Django custome login

in your urls.py
===============================================================================
from django.urls import path
from . import views

app_name = "main"   


urlpatterns = [
    path("", views.homepage, name="homepage"),
    ...
    path("register", views.register_request, name="register"),
    path("login", views.login_request, name="login")
]
================================================================================
in your views.py
================================================================================

from django.shortcuts import  render, redirect
from .forms import NewUserForm
from django.contrib.auth import login, authenticate #add this
from django.contrib import messages
from django.contrib.auth.forms import AuthenticationForm #add this

def register_request(request):
	...

def login_request(request):
	if request.method == "POST":
		form = AuthenticationForm(request, data=request.POST)
		if form.is_valid():
			username = form.cleaned_data.get('username')
			password = form.cleaned_data.get('password')
			user = authenticate(username=username, password=password)
			if user is not None:
				login(request, user)
				messages.info(request, f"You are now logged in as {username}.")
				return redirect("main:homepage")
			else:
				messages.error(request,"Invalid username or password.")
		else:
			messages.error(request,"Invalid username or password.")
	form = AuthenticationForm()
	return render(request=request, template_name="main/login.html", context={"login_form":form})
Comment

PREVIOUS NEXT
Code Example
Python :: python code with sigma 
Python :: how to install pyinstaller 
Python :: remove dot from number python 
Python :: findout not common values between two data frames 
Python :: python split string size 
Python :: python declare a variable 
Python :: variable in python 
Python :: django slug int url mapping 
Python :: python check if dataframe series contains string 
Python :: python isset 
Python :: python align label left 
Python :: how to convert .ui file to .py 
Python :: escape character in python 
Python :: set allowed methods flask 
Python :: pandas legend placement 
Python :: how to create adjacency matrix from adjacency list in python 
Python :: selenium webdriver scroll down python 
Python :: how to get local ip in python 
Python :: seir model python 
Python :: change dataframe to list 
Python :: python argparse custom categories 
Python :: def extract_title(input_df): 
Python :: how to view all attributes and methods of an object python 
Python :: python negative indexing 
Python :: euclidean algorithm recursive python 
Python :: get last 3 elements in a list python 
Python :: py foreach 
Python :: matplotlib different number of subplots 
Python :: save turtle programming python 
Python :: bar plot bokeh 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =