我正在将用户名和密码从模板传递到我的 views.py 并尝试将其保存在我的数据库中,但它给出了错误 - 此构造函数不带任何参数
我的观点.py -
def home(request):
if request.method == 'POST':
post = request.POST
if 'submit' in post:
u = user(username = post['username'], password = post['password'])
u.save()
return render(request, 'XO/home_page.html')
我的模型.py -
class user:
username = models.CharField(max_length = 30)
password = models.CharField(max_length = 30)
def __str__(self):
return self.username
我的模板-
<html>
<body>
<form id="form" method="post">
{% csrf_token %}
Username: <input type="text" name="username"/>
Password: <input type="text" name="password"/>
<button type="submit" name="submit">
Submit
</button>
</form>
</body>
</html>
知道为什么我会收到此错误......?
提前致谢。
最佳答案
您应该从 models.Model
类继承模型:
class user(models.Model):
...
另请注意,您有一个缩进问题:__str__()
方法应该在 user
类内部的正确一级。
关于python - 类型错误 : this constructor takes no arguments in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29276939/