Python 类型错误:__init__() 需要 1 个位置参数,但给出了 3 个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30032078/
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
TypeError: __init__() takes 1 positional argument but 3 were given
提问by gaurav bajaj
I know this title look familiar to some old questions, but I've looked at every single one of them, none of them solves. And here is my codes:
我知道这个标题对于一些老问题来说很熟悉,但我已经看过了其中的每一个,但没有一个能解决。这是我的代码:
TypeError: __init__() takes 1 positional argument but 3 were given
Traceback (most recent call last)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\hp user\PycharmProjects\flask123\views.py", line 19, in create
create_post = Post(my_form.title.data, my_form.text.data)
TypeError: __init__() takes 1 positional argument but 3 were given
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
My classes are as follow:
我的课程如下:
Models.py
模型.py
from app import db
from datetime import datetime
class Post(db.Model):
post_id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
text = db.Column(db.Text())
created_time = db.Column(db.DateTime())
def __init__(self, title, text, created_time=None):
self.title = title
self.text = text
if created_time is None:
self.created_time = datetime.utcnow()
else:
self.created_time = created_time
views.py
视图.py
from app import app, db
from flask import render_template, request, url_for
from forms import CreateForm
from models import Post
@app.route('/')
def homepage():
return render_template('base.html')
@app.route('/create', methods=['GET', 'POST'])
def create():
form = CreateForm(csrf_enabled=False)
if request.method == 'GET':
return render_template('create.html', form=form)
if request.method == 'POST':
if form.validate_on_submit():
create_post = Post(form.title.data, form.text.data)
db.session.add(create_post)
db.session.commit()
return redirect(url_for('homepage'))
I have tried with all possible solution and checked my code for spelling mistakes.But I found none.
我已经尝试了所有可能的解决方案并检查了我的代码是否有拼写错误。但我没有发现。
采纳答案by Celeo
The error
错误
TypeError: __init__() takes 1 positional argument but 3 were given
类型错误:__init__() 需要 1 个位置参数,但给出了 3 个
happens at the code
发生在代码处
create_post = Post(my_form.title.data, my_form.text.data)
Instead of passing position arguments to the creation of the Post
object, pass keyword arguments:
不是将位置参数传递给Post
对象的创建,而是传递关键字参数:
create_post = Post(title=my_form.title.data, text=my_form.text.data)