python - 类型错误 : this constructor takes no arguments in django

标签 python django

我正在将用户名和密码从模板传递到我的 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/

相关文章:

python - 运行时错误: maximum recursion depth exceeded in Odoo 10

python - 在 Heroku 上找不到与 torch==1.5.0+cpu 匹配的分布

python - 为什么我在尝试导入 JSON 时收到 IntegrityError?

python - Django 迁移损坏,不会忘记已删除的列

python - mod_wsgi 出现 Django 500 错误

python - 将 id 传递给 url 链接 - Django

python - 如何在 Python 上关闭注册表重定向?

python - 通过唯一标签合并 networkx 中的两个网络映射

python - 如何在 tensorflow 中获取当前可用的 GPU?

python - 为什么 full_like 这么慢(与其他方法相比)?