【发布时间】:2020-10-17 03:21:17
【问题描述】:
我收到一个错误查询集对象没有属性“密码”。谁能帮助我如何比较用户输入的密码和返回
def login(request):
if request.method == 'GET':
return render (request, 'login.html')
else:
email = request.POST.get('email')
password = request.POST.get('password')
print(email,password)
#Now we will try to match user entered email ID and search it in db(here we Can't use pass because it is in ecrypted mode we can see from admin panel
# to filter result we can use Customer.objects.filter which will return match in list form but we want a single object so better is to use get
# Customer.objects.get(email=email))
#drawback of get is that if result is matched then good else it will give error.
login_customer = Customer.objects.filter(email=email)
print(login_customer)
print('-------')
error = None
if login_customer:
print(email)
flag = check_password(password, login_customer.password)
#if user email ID is exit then we'll Check his password.:)
if flag:
return redirect('home')
else:
print(email, password)
error = 'Entered Email ID OR Password is incorrect'
return render(request, 'login.html',{'error':error})
customer.py(模型):
from django.db import models
##Customer Model Creation.
# Create your models here.
class Customer(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
phone = models.CharField(max_length=15)
email = models.EmailField()
password = models.CharField(max_length=250)
案例 b:当我使用 get 而不是过滤器时。
def login(request):
if request.method == 'GET':
return render (request, 'login.html')
else:
email = request.POST.get('email')
password = request.POST.get('password')
print(email,password)
#Now we will try to match user entered email ID and search it in db(here we Can't use pass because it is in ecrypted mode we can see from admin panel
# to filter result we can use Customer.objects.filter which will return match in list form but we want a single object so better is to use
# Customer.objects.get(email=email))
#drawback of get is that if result is matched then good else it will give error.so better is to use
login_customer = Customer.objects.get(email=email)
print(login_customer)
print('-------')
error = None
if login_customer:
print(email)
print('+++++')
flag = check_password(password, login_customer.password)
#if user email ID is exit then we'll Check his password.:)
if flag:
return redirect('home')
else:
print(email, password)
error = 'Entered Email ID OR Password is incorrect'
return render(request, 'login.html',{'error':error})
问题:它没有重定向到主页我认为这行有问题(*flag = check_password(password, login_customer.password)*)。
终端输出:
Django version 3.0.2, using settings 'ShaileshShop.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
sheru@apple.com None
Customer object (42)
-------
sheru@apple.com
+++++
[17/Oct/2020 03:14:26] "POST /login HTTP/1.1" 200 5167
#如何加密密码
from django.contrib.auth.hashers import make_password,check_password
def signup(request):
if request.method == 'GET':
print(request.method)
return render(request,'signup.html')
else:
postdata=request.POST
first_name = postdata.get('firstname')
last_name = postdata.get('lastname')
phone = postdata.get('PhoneNumber')
email = postdata.get('email')
password = postdata.get('Password')
#Now to store filled data if any error is coming so that User not required to fill it again.####
value={
'first_name' : first_name,
'last_name' : last_name,
'phone' : phone,
'email' : email,
#password not passing :) user need to fill it hahhahaa
}
####Validating above field at server level####
error = None
customer = Customer(first_name=first_name,
last_name=last_name,
phone=phone,
email=email,
password=password)
if not first_name:
error = 'First name is required!!'
elif len(first_name) < 4:
error = 'First Name must be 4 char length'
elif not last_name:
error ='Last Name is required!.'
elif len(last_name) < 4:
error = 'Last Name Must be 4 Character long.'
elif len(phone) < 10:
error ='Mobile number should be of 10 digit.'
elif Customer.objects.filter(phone=phone):
error = "Mobile no is already registered."
elif len(password) < 5:
error ="Password must be 5 Char length."
elif len(email) <6:
error = "Email Id must be more than 6 Character !"
elif Customer.objects.filter(email=email):
error = "Sorry You already have account with this Email ID."
if not error:
print(first_name,last_name,email,password,phone)
customer.password = make_password(customer.password)
customer.save()
#best way is to go into urls.py and define name=hompage because tommorow if we'll upload on production this domain will change.
return redirect('home')
# return redirect('http://127.0.0.1:8000')this is not recommonded
# return render(request,'index.html')#paasing data otherwise product will not shown to us after redirecting to index.
# in this we will not get all product image so we need to check how we can use above data already written code
else:
data = {
'error': error,
'values': value,
}
return render(request,'signup.html', data)
【问题讨论】:
-
在您的登录视图中,
print(login_customer.password)的结果是什么? -
它以加密形式给我密码 print(login_customer.password) pbkdf2_sha256$180000$BUsJchRCQdo0$S4WD7GNZNMhAT9a7UBu5FX0qra8rHpuZXYG3q+/nniw=(在数据库中签入也同样通过。)------------ -------------------------------------------------- --------------- x=make_password(password) print(x) o/p:!Ba41NTEVRFHUpyPu43MHy7ss58a4esWChbIWEp8l
标签: python django django-views