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
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.
Comments