How to Login, Logout and User Registration through in build Authentication method?

user6932238

view.py

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages

# Create your views here.
def signin(request):
    if request.method=='POST':
        username=request.POST['username']
        password=request.POST['password']
        user=authenticate(username=username,password=password)
        if user !=None:
            login(request,user)
        else:
            return HttpResponseRedirect('signin')
    else:
        messages.add_message(request, messages.ERROR, "Incorrect user or password")
    return render(request,'customer/login.html')

def signout(request):
    logout(request)

@login_required(login_url='customer/login.html')
def createcustomer(request):
    if request.method == 'POST':
        form = CustomerForm(request.POST)
        if form.is_valid():
            customer_save=Customer.objects.create(
                fname=form.cleaned_data['fname'],
                lname = form.cleaned_data['lname'],
                email= form.cleaned_data['email'],
                address= form.cleaned_data['address'],
                city=form.cleaned_data['city'],
                state=form.cleaned_data['state'],
                zip=form.cleaned_data['zip'],
                uname=form.cleaned_data['uname'],
                password=form.cleaned_data['password'],
                age=form.cleaned_data['age'],
                mobile=form.cleaned_data['mobile'],
                phone=form.cleaned_data['phone'],
            )
            customer_save.save()

            return HttpResponseRedirect('thanks')
    else:
        form = CustomerForm()

    return render(request, 'customer/createcustomer.html', {'form': form})

login.html

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Custmer Tasks</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <title>Login</title>
</head>
<body>
<div class="container-fluid" style="background-color: #f2f2f2;border:solid;" >
    <div class="col-md-4"></div>
    <div class="col-md-4">
        <form class="login-page" method="post" action="">

            <div class="input-group margin-bottom-20">
                <span class="input-group-addon">
                    <i class="fa fa-user"></i>
                </span>
                <input type="text" name="username" class="form-control" placeholder="Username/Email">
            </div>
            <div class="input-group margin-bottom-20">
                <span class="input-group-addon">
                    <i class="fa fa-lock"></i>
                </span>
                <input type="password" name="password" class="form-control" placeholder="Password">
            </div>
            <div class="row">
                <div class="col-md-6">
                    <label class="checkbox">
                        <input type="checkbox">Stay signed in</label>
                </div>
                <div class="col-md-6">
                    <button class="btn btn-primary pull-right" type="submit">Login</button>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
</html>

url.py

from  views import signin
from django.contrib.auth import views as auth_views
from django.conf.urls import url
from django.contrib.auth.views import login
from views import createcustomer, customerlist, customerdetails,thanks,   editcustomer, createtasks, listtasks, edittasks, viewtasks, customertable, tasktable
from  views import signin
from django.contrib.auth import views as auth_views

urlpatterns = [
url(r'^signin$', signin, name="signin"),
url(r'^customer/login/$', auth_views.login),
#url(r'^login/$', login, name="login"),
url(r'^create/$', createcustomer, name="createcustomer"),
url(r'^list/$', customerlist, name="customerlist"),
url(r'^edit/(?P<pk>[0-9]+)/', editcustomer, name="editcustomer"),
url(r'^view/(?P<id>[0-9]+)/', customerdetails, name="customerdetails"),
 ......
]

What I am Doing is: I want to add a customer from a web page to database, but i want that only registerd person can post the Customer details in database. So i want to add new authenticate user using authnticate method. Login the authenticate user, then uthenticate user cand add after login.

Basically i want a simple sign in, sign out and registrasion using Authentication. Please comment or send me mail ([email protected]) if you want other code

Amandeep Dhiman

Use this code for signin in views.py file

def signin(request):
if request.method=='POST':
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
         login(request, user)
         return HttpResponseRedirect('/customer/create/')
    else:
        return render(request, 'customer/createcustomer.html', { "user": user})
else:
    user=UserForm()
    return render(request, 'customer/login.html', {"user": user, })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

GCM , Registration ID and handling user login/logout

How to logout a user from the login page?

How to Create login and logout using Vapor (Basic Authentication)

how to change login button to logout after the user login PHP

Is JWT authentication needed for login/logout

CakePHP Authentication (login / logout) not working

User registration and authentication

How to handle User login service to get user info and logout state

How implement Flutter login authentication & registration using Http post

Android User Login and Registration

No user registration on login screen

How to logout user using Facebook authentication using Swift and iOS?

Not able to logout user django authentication

How to keep user login in to system and logout only after user clicks on logout button?

How to logout google authentication?

How i get, is user login or logout in facebook sdk 4.0.1

How can i redirect to login if user tries logout route directly

Fabric: logout and then login as a new user

Script To Logout Root and Login User

Spring MVC default Authentication How to add a user / registration

Firebase User Registration / Authentication with Username

How to enable a user to login through FTP?

How to prevent user login automatically after registration in Laravel 5.5

How to add user roles dropdown in registration and login woocommerce wordpress

Django: How to login user directly after registration using generic CreateView

How to customise drupal 7 user registration, login, password reset form

Login User with Firebase Authentication

User login authentication

Save GCM Registration And Login User