Flask

Flask raises TemplateNotFound error even though template file exists

一笑奈何 提交于 2021-01-29 06:11:14
问题 I am trying to render the file home.html . The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html when I try to render it. Why can't Flask find my template? from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') /myproject app.py home.html 回答1: You must create your template files in the correct location; in the templates subdirectory next to the python module (== the module where

How to pass variables in python flask to mysqldb?

风流意气都作罢 提交于 2021-01-29 05:21:44
问题 Code in .py file: cur = mysql.connection.cursor() # Check if this user had voted for somebody is_voted = cur.execute("SELECT TUTOR_VOTED FROM USERS WHERE USERNAME="+str(session["username"])) session["username"] keep a user cookie. The user I already logged in names "admin" But there might be something wrong with the MySQL command inside is_voted Error: MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'admin' in 'where clause'") But I got the correct return value while using SELECT

Flask & MongoDB - For Loop not working

戏子无情 提交于 2021-01-29 03:14:54
问题 Having this for Flask: example = mongo.db.example got_name = example.find({'name':1}) got_lastname = example.find({'lastname':1}) details = {'name' : got_name, 'lastname' : got_lastname} return render_template('blabla.html', details=details) Then the for loop using Jinja in my HTML (wanting it to be a table): {% for x in details} <tr> <td>{{ x.['name'] }}</td> <td>{{ x.['lastname'] }}</td> </tr> {% endfor %} But it won't work, it doesn't display anything in my table. I wrote this example

flask reload module on windows

ぐ巨炮叔叔 提交于 2021-01-29 03:14:12
问题 I am on Windows, using python 2.7, flask installed with pip. I am trying to get flask reload my python module after source code change. I did SET FLASK_APP=backend.py set FLASK_DEBUG=1 flask run this causes the following error: * Serving Flask app "backend" * Forcing debug mode on * Restarting with stat c:\python27\python.exe: can't open file 'C:\Python27\Scripts\flask': [Errno 2] No such file or directory So I tried (as suggested by some others) python -m flask run which however caused the

Deploying Flask app to via Elastic Beanstalk which interacts with S3

拟墨画扇 提交于 2021-01-29 02:26:02
问题 I have a Flask app which looks like this: from flask import Flask import boto3 application = Flask(__name__) @application.route("/") def home(): return "Server successfully loaded" @application.route("/app") def frontend_from_aws(): s3 = boto3.resource("s3") frontend = s3.Object(bucket_name = "my_bucket", key = "frontend.html") return frontend.get()["Body"].read() if __name__ == "__main__": application.debug = True application.run() Everything works perfectly when I test locally, but when I

SQLAlchemy automap - adding methods to automapped Model

我与影子孤独终老i 提交于 2021-01-29 02:04:13
问题 I have a preexisting database that I'm using with SQLAlchemy, so I'm using automap to get the Models from the database. What is the best way to add methods to these classes? For example, for a User class, I'd like to add methods such as verifying the password. Also, I'd like to add methods for flask-login (UserMixin) methods. 回答1: Specify your classes explicitly beforehand, and define your methods as you would normally: Base = automap_base() class User(Base): __tablename__ = 'user' def verify

SQLAlchemy automap - adding methods to automapped Model

这一生的挚爱 提交于 2021-01-29 01:59:45
问题 I have a preexisting database that I'm using with SQLAlchemy, so I'm using automap to get the Models from the database. What is the best way to add methods to these classes? For example, for a User class, I'd like to add methods such as verifying the password. Also, I'd like to add methods for flask-login (UserMixin) methods. 回答1: Specify your classes explicitly beforehand, and define your methods as you would normally: Base = automap_base() class User(Base): __tablename__ = 'user' def verify

Thread that calls function in a HTTP request throws RuntimeError: Working outside of application context

会有一股神秘感。 提交于 2021-01-28 21:15:46
问题 I have route within my Flask application that process a POST request and then sends an email to the client using the Flask-Mail library. I have the route returning a response to the client from checking the database before sending out an email. The email is sent from an individual thread, but I am getting this error thrown when trying to send out the email: RuntimeError: Working outside of application context. . I know that it is being thrown because the request was destroyed before the

Thread that calls function in a HTTP request throws RuntimeError: Working outside of application context

时光总嘲笑我的痴心妄想 提交于 2021-01-28 20:31:35
问题 I have route within my Flask application that process a POST request and then sends an email to the client using the Flask-Mail library. I have the route returning a response to the client from checking the database before sending out an email. The email is sent from an individual thread, but I am getting this error thrown when trying to send out the email: RuntimeError: Working outside of application context. . I know that it is being thrown because the request was destroyed before the

Python 3.7.0 Heroku buildpack issue

我是研究僧i 提交于 2021-01-28 19:51:59
问题 I've read some people with the same issue, but nothing suggested has worked. I'm trying to deploy a silly project to Heroku but nothing is working. Below this lines you can see the message: Writing objects: 100% (100/100), 55.42 MiB | 625.00 KiB/s, done. Total 100 (delta 19), reused 4 (delta 0) remote: Compressing source files... done. remote: Building source: Counting objects: 100, done. Delta compression using up to 4 threads. Compressing objects: 100% (94/94), done. Writing objects: 100%