[英]__init__() got an unexpected keyword argument 'password' django
I am using ajax calls in my program. 我在程序中使用了ajax调用。 I am getting errors like: 我收到如下错误:
TypeError: __init__() got an unexpected keyword argument 'password'
I have following in model: 我在模型中有以下内容:
class Student(models.Model):
name = models.CharField(max_length = 20 )
password = models.CharField(max_length = 100 )
email = models.CharField( max_length = 10 )
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__
Now, the following is my template: 现在,以下是我的模板:
`<form id = "post_submit" action="{% url "apply" %}" method="POST">
{% csrf_token %}
{% for field in form %}
<p>{{field.label}} : {{field}}</p>
{% endfor %}
<p><input type="submit" name="Submit"></p>
</form>
<p id="click_option">Click here.</p>
Ajax calls are made using: Ajax调用使用以下命令进行:
$.ajax({
// using this for csrf handling
// alert(" i am in ajax");
// console.log(" iam ");
url : "/apply/",
type : "POST",
data :
{
csrfmiddlewaretoken:document.getElementsByName('csrfmiddlewaretoken')[0].value,
name : $('#id_name').val(),
email : $('#id_email').val(),
password : $('#id_password').val(),
},
success: function(json) {
alert("Congratulations! You scored: " + json['status']);
},
// error
})
});
the view in backend is: 后端的视图是:
def apply(request): def apply(要求):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
password = request.POST.get('password')
student = StudentForm(name = name, email = email, password = password)
student.save()
data = {"status" : "success"}
return JsonResponse(data)
else:
data = {"status" : "failure"}
return JsonResponse(data)
You don't have to manually fetch the values out of the POST data, the form takes care of that for you. 您不必手动从POST数据中获取值,该表单将为您处理这些事情。
After checking if the form is valid, you can call save()
to save the model instance. 检查表单是否有效后,可以调用save()
保存模型实例。
form = StudentForm(request.POST)
if form.is_valid()
student = form.save()
Finally, your Student
model is storing a password
in a CharField
. 最后,您的Student
模型将password
存储在CharField
。 It is insecure to store passwords in plain text. 用纯文本存储密码是不安全的。 Django's authentication system takes care of hashing passwords for you. Django的身份验证系统会为您处理哈希密码。 Use it. 用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.