Login In Django

[Solved] Login In Django | Php - Code Explorer | yomemimo.com
Question : django login code

Answered by : hervi-may

from django.contrib.auth import authenticate, login
def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid login' error message. ...

Source : | Last Update : Mon, 11 Apr 22

Question : django login page

Answered by : vipin-yadav

from django.contrib.auth import authenticate, login
fro django.shortcuts import render, redirect
def login_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. return redirect('/') ... else: # Return an 'invalid login' error message. ... context = {'error':'Invalid username or password.'} return render(request, '/login.html', context) 

Source : | Last Update : Sat, 03 Sep 22

Question : login in django

Answered by : manish-bhusal

def signup(request): if request.method == 'POST': full_name = request.POST['name'].split() first_name = full_name[0] if len(full_name) == 1: last_name = '' if len(full_name) == 2: last_name = full_name[1] if len(full_name) > 2: last_name = full_name[1:] last_name = ' '.join(last_name) username = request.POST['username'] email = request.POST['email'] password = request.POST['password'] confirm_password = request.POST['confirm_password'] if password == confirm_password: if User.objects.filter(username=username).exists(): # Add an information message to the current request messages.info(request, 'Username already exists') # Redirect the user to the 'home' page return redirect('home') elif User.objects.filter(email=email).exists(): # Add an information message to the current request messages.info(request, 'Email already exists') # Redirect the user to the 'home' page return redirect('home') else: # Create a new User object with the provided data user = User.objects.create_user( username=username, password=password, email=email) user.first_name = first_name user.last_name = last_name user.save() # Add a success message to the current request messages.success(request, 'User created successfully') # Redirect the user to the 'home' page return redirect('home') else: # Add an information message to the current request messages.info(request, 'Password not matching') return redirect('home') # Return an HTTP response with the message "Page not found" if the request method is not 'POST' return render(request, 'portfolio/404_error.html')
def user_login(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] # Authenticate the user with the given username and password user = authenticate(username=username, password=password) # If the user is authenticated, log them in and redirect to the home page if user is not None: login(request, user) messages.success(request, 'Login Successful') return redirect('home') # If the user is not authenticated, display an error message and redirect to the home page else: messages.info(request, 'Invalid Credentials') return redirect('home') # If the request method is not 'POST', render the 404 error page return render(request, 'portfolio/404_error.html')
def signout(request): # Log the user out logout(request) # Display a success message messages.success(request, 'Logged out successfully!') # Redirect the user to the home page return redirect('home')

Source : | Last Update : Wed, 12 Jul 23

Question : login view django

Answered by : clever-cockroach-ybkgcxpg00vu

from django.contrib import messages
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import render, redirect
def login_view(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 ('inventory:home') else: messages.error(request, "Invalid username or password") else: messages.error(request, "Invalid username or password") form = AuthenticationForm() return render(request, 'registration/login.html', context={"login_form":form})

Source : | Last Update : Mon, 14 Nov 22

Question : django login

Answered by : gleaming-guanaco-ueyoov87i1g9

def login_view(request): if request.method == 'GET': cache.set('next', request.GET.get('next', None)) if request.method == 'POST': # do your checks here login(request, user) next_url = cache.get('next') if next_url: cache.delete('next') return HttpResponseRedirect(next_url) return render(request, 'account/login.html')

Source : https://stackoverflow.com/questions/38431166/redirect-to-next-after-login-in-django/64959042 | Last Update : Thu, 28 Oct 21

Question : login view django

Answered by : manish-bhusal

{"tags":[{"tag":"textarea","content":"from django.contrib.auth.views import LoginView\nfrom django.urls import reverse_lazy\nfrom django.contrib import messages\n\n\nclass MyLoginView(LoginView):\n redirect_authenticated_user = True\n \n def get_success_url(self):\n return reverse_lazy('tasks') \n \n def form_invalid(self, form):\n messages.error(self.request,'Invalid username or password')\n return self.render_to_response(self.get_context_data(form=form))\nCode language: Python (python)","code_language":"python"}]}

Source : https://www.pythontutorial.net/django-tutorial/django-loginview/ | Last Update : Tue, 30 May 23

Question : login system in django

Answered by : fair-frog-qonwftra2f2g

LOGOUT_REDIRECT_URL = 'your_url'

Source : | Last Update : Mon, 11 Jan 21

Answers related to login in django

Code Explorer Popular Question For Php