Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

django-login

Why second user login redirects me to /accounts/profile/ url in Django?

November 20, 2023 by Tarik

django.contrib.auth.views.login redirects you to accounts/profile/ right after you log in. You may want to set LOGIN_REDIRECT_URL inside your settings.py file to anything you like..

Categories python Tags django, django-authentication, django-login, python, redirect Leave a comment

Set all pages to require login, globally?

August 18, 2023 by Tarik

from django.shortcuts import redirect from django.conf import settings class LoginRequiredMiddleware: def __init__(self, get_response): self.get_response = get_response self.login_url = settings.LOGIN_URL self.open_urls = [self.login_url] + \ getattr(settings, ‘OPEN_URLS’, []) def __call__(self, request): if not request.user.is_authenticated \ and not request.path_info in self.open_urls: return redirect(self.login_url+’?next=”+request.path) return self.get_response(request)

Categories django Tags django, django-login, redirect Leave a comment

Log in user using either email address or username in Django

August 6, 2023 by Tarik

Yet another solution: from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend from django.db.models import Q class EmailOrUsernameModelBackend(ModelBackend): “”” Authentication backend which allows users to authenticate using either their username or email address Source: https://stackoverflow.com/a/35836674/59984 “”” def authenticate(self, request, username=None, password=None, **kwargs): # n.b. Django <2.1 does not pass the `request` user_model = get_user_model() if username … Read more

Categories python Tags django, django-authentication, django-login, python Leave a comment

Django – Login with Email

December 19, 2022 by Tarik

You should write a custom authentication backend. Something like this will work: from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend class EmailBackend(ModelBackend): def authenticate(self, request, username=None, password=None, **kwargs): UserModel = get_user_model() try: user = UserModel.objects.get(email=username) except UserModel.DoesNotExist: return None else: if user.check_password(password): return user return None Then, set that backend as your auth backend in … Read more

Categories python Tags django, django-authentication, django-login, python, python-3.x Leave a comment

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa