语法错误def __repr __()

时间:2018-07-11 15:38:11

标签: python-3.x syntax-error flask-sqlalchemy

我正在研究Flask Web Development一书。我现在被困住了,无法自救。

这是我的代码中导致问题的部分。

import os
from flask_script import Manager
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from wtforms import StringField, SubmitField, validators
from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy

class NameForm(FlaskForm):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.Column('User',backref='role') 

    def  __repr__(self):
        return '<Role %r>' % self.name

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Interger, db.ForeignKey('roles.id')

    def  __repr__(self):
        return '<User %r>' % self.username

当我尝试启动外壳程序时,出现以下错误消息:

line 30
def  __repr__(self):
  ^
SyntaxError: invalid syntax

请问问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您在此行中缺少右括号()

role_id = db.Column(db.Interger, db.ForeignKey('roles.id')