Code Monkey home page Code Monkey logo

authentication-in-django's Introduction

Django-Authentication

1. Import "from django.contrib.auth import logout, authenticate, login" (for making login and logout in page).

2. First, authenticate (using the 'authenticate' method) the username and password from values submitted by the user. If username and password are correct then this function will returns the user object , otherwise it will returns None .

3. For login the user

If the user is not none ( username and password are correct ) then login the user using (login function) and redirect the user to the desired page. Otherwise, redirect the user to the login page again.

Example:

def login_user(request):
   if request.method=="POST":
      username=request.POST.get('username')
      passwd=request.POST.get('password')
      user=authenticate(username=username , password=passwd)
      if user is not None:
         login(request , user)
         return redirect('/')
      else:
         return render(request ,  'login.html')
    return render(request ,  'login.html')

4. For the home page :

If the user is anonymous then render the login page to the user. otherwise, render the content template to the user.

Example:

 def index (request):
    if request.user.is_anonymous:
       return render(request , 'login.html')
    return render(request , 'index.html') 

5. For logout

If user wants to logout from the page the logout the session using (logout function)

Example:

       def logout_user(request):
          logout(request )
          return redirect('/login')

authentication-in-django's People

Contributors

gaurav0401 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.