Python 类型错误:save() 缺少 1 个必需的位置参数:'self'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46907511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:55:00  来源:igfitidea点击:

TypeError: save() missing 1 required positional argument: 'self'

pythondjango

提问by kerberos

this is views

这是意见

class RegisterView(View):
def get(self,request):
    register_form = RegisterForm()
    return render(request,'register.html',{'register_form':register_form})
def post(self,request):
    register_form = RegisterForm(request.POST)
    if register_form.is_valid():
        user_name = request.POST.get("email", '')
        pass_word = request.POST.get("password", '')
        user_profile = UserProfile
        user_profile.username = user_name
        user_profile.email = user_name
        user_profile.password = make_password(pass_word)
        user_profile.save()   #error
        send_register_email(user_name,"register")

I want to save user_profile to mysql,But user_profile.save() has an error, TypeError: save() missing 1 required positional argument: 'self',How should I solve it?

我想将 user_profile 保存到 mysql,但是 user_profile.save() 有错误, TypeError: save() missing 1 required positional argument: 'self',我该如何解决?

回答by Anbarasan

you have not instantiated an object of UserProfile, instead you are assigning UserProfileto user_profile

您尚未实例化 的对象UserProfile,而是分配UserProfileuser_profile

user_profile = UserProfile

user_profile = UserProfile

should be

应该

user_profile = UserProfile()

user_profile = UserProfile()