简体   繁体   English

__init __()获得了意外的关键字参数'password'django

[英]__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.

相关问题 Django 表单密码 TypeError: __init__() got an unexpected keyword argument 'widget' - Django form password TypeError: __init__() got an unexpected keyword argument 'widget' __init __()获得了意外的关键字参数&#39;save&#39;-Django Python - __init__() got an unexpected keyword argument 'save' - django python Django __init __()使用CreateView获得了意外的关键字参数 - Django __init__() got an unexpected keyword argument using CreateView django __init __()获得了意外的关键字参数&#39;content&#39; - django __init__() got an unexpected keyword argument 'content' Django 错误:TypeError:__init__() 得到了一个意外的关键字参数“attrs” - Django Error: TypeError: __init__() got an unexpected keyword argument 'attrs' __init__() 在 django 中得到了一个意外的关键字参数 'user_id' - __init__() got an unexpected keyword argument 'user_id' in django django __init __()获得了意外的关键字参数&#39;param&#39; - django __init__() got an unexpected keyword argument 'param' Django TypeError: __init__() 得到了一个意外的关键字参数“attrs” - Django TypeError: __init__() got an unexpected keyword argument 'attrs' __init__() 在 django 中出现意外的关键字参数“状态”错误 - __init__() got an unexpected keyword argument 'status' error in django Django Channels __init__() 有一个意外的关键字参数“范围” - Django Channels __init__() got an unexpected keyword argument 'scope'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM