Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: __init__()
#1
Hello, im new at python,and im learning how to develop a web app, with flask, and useing sql alchemy, so i have a form to register members to my author table, but im recibing the following error:
TypeError: __init__() takes 1 positional argument but 6 were given

i have the folders author, and blog

so in my author folder i have my models .py as follow


 from flask_blog import db


class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String(80))
    email = db.Column(db.String(35), unique=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(80))
    is_author = db.Column(db.Boolean)


def __init__(self, fullname, email, username, password, is_author=False):

    self.fullname = fullname
    self.email = email
    self.username = username
    self.password = password
    self.is_author = is_author


def __repr__(self):
    return '<Author %r>' % self.username
so the problem appears in my views.py which is in my blog folder
 
from flask_blog import app
from flask import render_template, redirect, flash, url_for  # noqa : F401
from blog.form import SetUpForm
from flask_blog import db
from author.models import Author
from blog.models import Blog


@app.route('/')
@app.route('/index')
def index():
        return"Hello World"


@app.route('/admin')
def admin():
        blogs = Blog.query.count()
        if blogs == 0:
                return redirect(url_for('setup'))
        return render_template('blog/admin.html')


@app.route('/setup', methods=('GET', 'POST'))
def setup():
        error = ""
        form = SetUpForm()
        if form.validate_on_submit():
                author = Author(
                        form.fullname.data,
                        form.email.data,
                        form.username.data,
                        form.password.data,
                        True
                )
                db.session.add(author)
                db.session.flush()
                if author.id:
                        blog = Blog(
                                form.name.data,
                                author.id
                        )
                        db.session.add(blog)
                        db.session.flush()
                else:
                        db.session.rollblack()
                        error = "Error creating user"
                if author.id and blog.id:
                        db.session.comit()
                        flash(" Blog created ")
                        return redirect(url_for('admin'))
                else:
                        db.session.rollback()
                        error = "Error creating blog "  # noqa : F841
        return render_template('blog/setup.html', form=form)
so i dont know where the problem is, cause my instance of Author, have 5 values, because the id is an autoincrmentable value, but somehow it is expecting one argument

Moderator: nilamo: Traceback was added in the other thread
Quote:
Error:
sorry miss the tracerback Traceback (most recent call last): File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\_compat.py", line 35, in reraise raise value File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\_compat.py", line 35, in reraise raise value File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\python\FLASK_~2\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\Ignacio Muriel\Desktop\programacion\python\flask_blog\blog\views.py", line 34, in setup True TypeError: __init__() takes 1 positional argument but 6 were given
Reply
#2
Please provide the full stack trace, not just a part of it. Also, the code you've posted has incorrect indentation, so please review your post for accuracy.
Reply
#3
In the future, please give the full traceback text for errors. It helps us find the problem.

I believe the problem is that for your Author class, __init__ and __repr__ are not part of the class. They need to be indented one more level, so they are part of the code block for the class.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
On second thought, the indentation isn't incorrect as I thought (would produce an indentation syntax error), I think that's what your problem is. In the Author code, you need to indent __init__ and the other method so that they're within the class. As-is, you have a default initializer method which only takes self as an argument.
Reply
#5
(Jan-09-2019, 08:26 PM)micseydel Wrote: Please provide the full stack trace, not just a part of it. Also, the code you've posted has incorrect indentation, so please review your post for accuracy.
sorry for that,i miss it.
(Jan-09-2019, 08:27 PM)ichabod801 Wrote: In the future, please give the full traceback text for errors. It helps us find the problem.

I believe the problem is that for your Author class, __init__ and __repr__ are not part of the class. They need to be indented one more level, so they are part of the code block for the class.
correct, newbie mistake Big Grin thanks a lot
(Jan-09-2019, 08:28 PM)micseydel Wrote: On second thought, the indentation isn't incorrect as I thought (would produce an indentation syntax error), I think that's what your problem is. In the Author code, you need to indent __init__ and the other method so that they're within the class. As-is, you have a default initializer method which only takes self as an argument.

correct, newbie mistake Big Grin thanks a lot
Reply
#6
Traceback was added to the other thread. I've copied it here into the OP.
Reply
#7
(Jan-09-2019, 08:26 PM)micseydel Wrote: Please provide the full stack trace, not just a part of it. Also, the code you've posted has incorrect indentation, so please review your post for accuracy.

(Jan-09-2019, 08:44 PM)nilamo Wrote: Traceback was added to the other thread. I've copied it here into the OP.

thanks, i was unable to edit this post
Reply
#8
Yeah, new members are pretty restricted on what ya'll can do, until you get a few posts under your belt. That particular restriction, is to prevent spam (creating a thread, then removing all the content with links to other sites).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020