我在努力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
所以我认为问题可能在导入语句中,或者至少在第一个路由之前:
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
此错误是因为您是循环导入应用程序(您在routes.py中导入了应用程序,在app中导入了routes.py)此模式不起作用且不正确。在flask中,您可以在单个文件中编写整个应用程序,也可以使用flask蓝图在模块中制作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()
问题内容: 有谁知道如果我有两个这样的URL规则,为什么我不能覆盖现有的终结点函数? 追溯: 问题答案: 你的视图名称即使指向相同的视图方法,也必须是唯一的。
问题内容: 是否有可能覆盖 全局 功能,从而在一定程度上影响 全局 功能? 据我所知,该函数在包装NodeJS脚本的函数中作为参数提供: 有什么方法可以修改功能吗? 这可能只会影响脚本所在的脚本。 我们如何在流程级别进行修改? 问题答案:
问题内容: 我有一个要覆盖的函数,但也有一个要根据上下文按顺序执行的功能。例如,有时在生成页面时,我会想要像这样覆盖: 有时像这样: 我如何从最重要的方面得到它?可能吗 我知道很多人都建议不要以这种方式替代替代。我在问这种方式。 问题答案: 您可以执行以下操作: 在匿名函数内部进行声明可以防止其混乱全局名称空间,但可以在内部函数中使用它。 就像评论中提到的Nerdmaster一样,一定要在最后加上
void fl_overlay_rect(int x, int y, int w, int h); void fl_overlay_clear(); 前者与先前颜色异或操作,后者清楚异或操作 使用该函数非常的巧妙,你应该在控件中有handle()和draw()函数,draw()应该调用fl_overlay_clear()在做任何事情之前。Handle()函数应该调用window()->make_
映射函数是一组可以连续应用于一个或多个元素列表的函数。 将这些函数应用于列表的结果将放在新列表中,并返回新列表。 例如, mapcar函数处理一个或多个列表的连续元素。 mapcar函数的第一个参数应该是一个函数,其余参数是应用函数的列表。 参数函数应用于导致新构造的列表的连续元素。 如果参数列表的长度不相等,则映射过程在到达最短列表的末尾时停止。 结果列表将具有与最短输入列表相同数量的元素。 例
我经常使用助手,我只是向用户返回带有消息的数据。现在我必须包括超文本传输协议状态代码,但我不想更改每个响应(这很可能是坏的)。 因此,我试图通过创建自己的