我试图通过http://code.tutsplus.com/tutorials/an-introduction-to-pythons-flask-framework--net-28822
努力C:\envs\virtalenvs\flask_mini\Scripts\python.exe C:/envs/r2/mini2/intro_to_flask/__init__.py
C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.')
Traceback (most recent call last):
File "C:/envs/r2/mini2/intro_to_flask/__init__.py", line 13, in <module>
from routes import mail
File "C:\envs\r2\mini2\intro_to_flask\routes.py", line 9, in <module>
@app.route('/')
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1013, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 984, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: home
routes.py文件是:
from intro_to_flask import app
from flask import render_template, request, flash, session, url_for, redirect
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Message, Mail
from models import db, User
mail = Mail()
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('All fields are required.')
return render_template('contact.html', form=form)
else:
msg = Message(form.subject.data, sender='contact@example.com', recipients=['your_email@example.com'])
msg.body = """
From: %s <%s>
%s
""" % (form.name.data, form.email.data, form.message.data)
mail.send(msg)
return render_template('contact.html', success=True)
elif request.method == 'GET':
return render_template('contact.html', form=form)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if 'email' in session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template('signup.html', form=form)
else:
newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
session['email'] = newuser.email
return redirect(url_for('profile'))
elif request.method == 'GET':
return render_template('signup.html', form=form)
@app.route('/profile')
def profile():
if 'email' not in session:
return redirect(url_for('signin'))
user = User.query.filter_by(email=session['email']).first()
if user is None:
return redirect(url_for('signin'))
else:
return render_template('profile.html')
@app.route('/signin', methods=['GET', 'POST'])
def signin():
form = SigninForm()
if 'email' in session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template('signin.html', form=form)
else:
session['email'] = form.email.data
return redirect(url_for('profile'))
elif request.method == 'GET':
return render_template('signin.html', form=form)
@app.route('/signout')
def signout():
if 'email' not in session:
return redirect(url_for('signin'))
session.pop('email', None)
return redirect(url_for('home'))
导致这种情况的原因是什么?
编辑:
当我首先使用about函数时,我得到:
AssertionError: View function mapping is overwriting an existing endpoint function: about
所以我认为问题可能出在import语句中,或者至少在第一条路径之前:
from intro_to_flask import app
from flask import render_template, request, flash, session, url_for, redirect
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Mail, Message
from models import db, User
mail = Mail()
编辑2:
from flask import Flask
app = Flask(__name__)
app.secret_key = 'development key'
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = 'contact@example.com'
app.config["MAIL_PASSWORD"] = 'your-password'
from routes import mail
mail.init_app(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/development'
from models import db
db.init_app(app)
import intro_to_flask.routes
答案 0 :(得分:2)
此错误是因为您是循环导入应用程序(您在routes.py中导入应用程序并在应用程序中导入routes.py) 此模式不起作用,并且不正确。 在烧瓶中,您可以将整个应用程序写入单个文件中,或者您可以使用烧瓶蓝图进行模块化 http://flask.pocoo.org/docs/0.10/blueprints/
您的应用程序可以在一个文件中工作:
from flask import render_template, request, flash, session, url_for, redirect
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Message, Mail
from models import db, User
from flask import Flask
app = Flask(__name__)
app.secret_key = 'development key'
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = 'contact@example.com'
app.config["MAIL_PASSWORD"] = 'your-password'
mail = Mail()
mail.init_app(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/development'
from models import db
db.init_app(app)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('All fields are required.')
return render_template('contact.html', form=form)
else:
msg = Message(form.subject.data, sender='contact@example.com', recipients=['your_email@example.com'])
msg.body = """
From: %s <%s>
%s
""" % (form.name.data, form.email.data, form.message.data)
mail.send(msg)
return render_template('contact.html', success=True)
elif request.method == 'GET':
return render_template('contact.html', form=form)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if 'email' in session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template('signup.html', form=form)
else:
newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
session['email'] = newuser.email
return redirect(url_for('profile'))
elif request.method == 'GET':
return render_template('signup.html', form=form)
@app.route('/profile')
def profile():
if 'email' not in session:
return redirect(url_for('signin'))
user = User.query.filter_by(email=session['email']).first()
if user is None:
return redirect(url_for('signin'))
else:
return render_template('profile.html')
@app.route('/signin', methods=['GET', 'POST'])
def signin():
form = SigninForm()
if 'email' in session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template('signin.html', form=form)
else:
session['email'] = form.email.data
return redirect(url_for('profile'))
elif request.method == 'GET':
return render_template('signin.html', form=form)
@app.route('/signout')
def signout():
if 'email' not in session:
return redirect(url_for('signin'))
session.pop('email', None)
return redirect(url_for('home'))
app.run()