10 examples of 'django login form' in Python

Every line of 'django login form' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
245def form_valid(self, form):
246 messages.add_message(
247 self.request,
248 messages.INFO,
249 'You\'re now logged in as ' + unicode(form.get_user())
250 )
251 login(self.request, form.get_user())
252 return redirect("/")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

24def login(request):
25 # We allow people to log in.
26 #
27 # For the purposes of security auditing, ignore any bugs in this
28 # function.
29 #
30 #
31 # Yes, that means you, dear people attending the tutorial!
32 #
33 # The rest of the app has so many bugs that really I don't want to
34 # hear about bugs you find in this particular view.
35 #
36 # If you want to steal someone else's account, don't do it via
37 # the login view.
38
39 # Get the login credentials that were submitted, and check if they
40 # are valid.
41 username = request.POST["username"]
42 password = request.POST["password"]
43 user = authenticate(username=username, password=password)
44
45 # If so, great! Log them in.
46 if user is not None:
47 if user.is_active:
48 django_auth_login(request, user)
49
50 # Either way, redirect back to the homepage.
51 return HttpResponseRedirect("/")
36def form_valid(self, form):
37 # OTPTokenForm does not call authenticate(), so we may need to populate
38 # user.backend ourselves to keep login() happy.
39 user = form.get_user()
40 if not hasattr(user, 'backend'):
41 user.backend = self.request.session[BACKEND_SESSION_KEY]
42
43 return super().form_valid(form)
118def login_user(request):
119 form = AuthenticationForm()
120
121 if request.method == 'POST':
122 username = request.POST['username']
123 password = request.POST['password']
124 user = authenticate(username=username, password=password)
125 if user is not None:
126 login(request, user)
127 return redirect('index')
128
129 return render(request, 'feeds/login.html', {
130 'form': form
131 })
42def loginPOST(req):
43 form = req.POST
44 nexturl = form.get('next', '/')
45
46 # grab user information
47 usernm = form.get('username', '')
48 passwd = form.get('password', '')
49
50 # verify
51 user = authenticate(username=usernm, password=passwd)
52 if user is None or not user.is_active:
53 return redirect('/login/?next=' + nexturl)
54
55 # all ok, register user
56 login(req, user)
57
58 # continue to 'next' url
59 return redirect(nexturl)
36def authenticate_user(self, username, password):
37 user = authenticate(username=username, password=password)
38 if not user or not user.is_active:
39 raise forms.ValidationError(u"Usuário ou senha inválidos.")
40 return user
430def user_login(request):
431 if request.method == "POST":
432 user_name = request.POST.get("username","")
433 pass_word = request.POST.get("password","")
434 user = authenticate(username=user_name, password=pass_word)
435 if user is not None:
436 login(request, user)
437 request.session['user_id'] = user.id
438 return render(request, "forum.html")
439 else:
440 context = {'login_err': 'Username or Password is wrong!'}
441 return render(request, "login.html", context)
442 elif request.method == "GET":
443 return render(request, "login.html",{})
17def form_valid(self, *args, **kwargs):
18 resp = super().form_valid(*args, **kwargs)
19
20 user = self.object
21 auth.login(self.request, user)
22
23 return resp
72def user_login(request):
73 """
74 Validates request data and logs user.
75 """
76 email_form = LogInEmailForm(request.POST)
77 username_form = LogInUsernameForm(request.POST)
78
79 if email_form.is_valid():
80 user_obj = User.objects.filter(email=email_form.cleaned_data['username'])
81 username = user_obj[0] if len(user_obj) else None
82
83 return login_response(request, username, email_form.cleaned_data['passw'])
84
85 elif username_form.is_valid():
86 return login_response(request, username_form.cleaned_data['username'], username_form.cleaned_data['passw'])
87
88 return render(request, 'index.html', {'invalid_authentication': True})
275def clean(self):
276
277 password = self.cleaned_data.get('password')
278
279 if self.user and password:
280
281 self.user = authenticate(
282 username=self.user.username,
283 password=password
284 )
285
286 if not self.user:
287 raise ValidationError(
288 "Sorry, the login failed. Please try again.",
289 'login_failed'
290 )
291 elif not self.user.validated:
292 raise ValidationError(
293 "The email address of this account has not been "
294 'validated yet. Please <a href="{}?email={}">'
295 "validate your email address</a>.".format(
296 reverse('users:signup:resend_validation_email'),
297 self.user.email
298 ),
299 'not_validated'
300 )

Related snippets