[英]Django: Password check returning false in view when it should return true
As part of form validation, the password and password2 are compared. 作为表单验证的一部分,将对password和password2进行比较。 This is part of a simplified ajax request system for an extremely small site returning small amounts of data (so i don't use JSON). 这是简化的ajax请求系统的一部分,该系统针对的是一个非常小的站点,它返回少量数据(因此我不使用JSON)。 The idea/objective/summery of the program is that it tries to log in the user. 该程序的想法/目的/摘要是它试图登录用户。 If the user does not exist, it asks the client side to load/reveal the form for new users (just an added password2 field and asks for a pen name). 如果用户不存在,它将要求客户端为新用户加载/显示表单(只需添加一个password2字段并要求输入笔名)。 Here is the file, I've marked the sport where the program freezes with #*********
这是文件,我用#*********
标记了程序冻结的运动
Forms.py (error is not here): Forms.py(错误不在这里):
class new_user(forms.Form):
username = forms.EmailField()
password = forms.PasswordInput()
password2 = forms.PasswordInput()
pen_name = forms.CharField(max_length=30)
Views.py, where the error is: Views.py,错误为:
def user_log_in(request):
#add stuff for ajax request
user_pass = log_in(request.POST)
er = []
if user_pass.is_valid():
print "valid"
cleaned_info = user_pass.cleaned_data
user_object = User.objects.filter(email = cleaned_info['username'])
if user_object.exists():
logged_in_user = auth.authenticate(username=cleaned_info['username'], auth_password=cleaned_info['password'])
#add in is_active
if logged_in_user is not None:
auth.login(request, logged_in_user)
return HttpResponseRedirect('/home')
else:
er.append("Incorrect Password")
else:
new_user_pass = new_user(request.POST)
if new_user_pass.is_valid():
cleaned_info_new = new_user_pass.cleaned_data
print "check points"
if cleaned_info_new['password'] == cleaned_info_new['password2']: #********
print "clean"
new_user_query = creat_user(
username=cleaned_info_new['username'],
password=cleaned_info_new['password'],
email=cleaned_info_new['username']
)
new_user_query.save()
msg = ""
try:
send_mail("Activate", msg, 'mrgnippy@gmail.com',
[cleaned_info['username']], fail_silently=False)
except:
er.append("Error Sending Email")
else:
er.append('Passwords are not the same')
elif "TN" in request.POST:
print "TN"
for e in new_user_pass.errors:
er.append(e)
#elif to check for is_active
else:
print "n_usr"
return HttpResponse('n_usr')
else:
for e in user_pass.errors:
er.append(e)
for e in er:
print"-"
print e
print"-"
return HttpResponse('SOS')
The django debug page says the following: django调试页面显示以下内容:
KeyError at /ajax/login 'password' Request Method: POST Request URL: http://127.0.0.1:8000/ajax/login Django Version: 1.4 Python Executable:
The post variable stuff in the error is this (I blanked out my email and): 错误中的post变量是这样的(我清空了电子邮件,然后):
GET: No GET data POST: username = u'********@aol.com' TN = u'TN' password2 = u'test' password = u'test' pen_name = u'testing123' FILES: No FILES data
Just in case the problem is here, I've included the javascript file. 万一问题出在这里,我已经包含了javascript文件。
n_usr = false;
function log_in () {
if(!$('#pass_enter').hasClass('#usr_enter')){
password = $('#pass_enter').val();
}else{
password = '';
}
if(!$('#usr_enter').hasClass('blur_field')){
username = $('#usr_enter').val();
}else{
username = '';
}
alert(username);
if(!n_usr){
$.post('/ajax/login',{password: password, username: username}, function(data) {
if(data == "n_usr"){
$('#new_user_entry').show('slow');
n_usr = true;
}
else {
}
})
}else {
if (!$('#pass_re_enter').hasClass('blur_field')){
password2 = $('#pass_re_enter').val();
}else {
password2 = '';
}
if (!$('#pass_re_enter').hasClass('blur_field')){
penname = $('#pen_enter').val();
}else {
penname = '';
}
$.post('/ajax/login', {password: password, password2: password2, username: username, pen_name: penname, TN: "TN"}, function(data) {
if(data == "e_act"){
} else {
}
});
}
}
I just noticed that I used the wrong fields in the forms.py file. 我只是注意到我在forms.py文件中使用了错误的字段。 Problem solved. 问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.