users我的MySql数据库中有表.这个表有id,name而且age领域.
我怎样才能删除一些记录id?
现在我使用以下代码:
user = User.query.get(id)
db.session.delete(user)
db.session.commit()
Run Code Online (Sandbox Code Playgroud)
但我不想在删除操作之前进行任何查询.有没有办法做到这一点?我知道,我可以使用db.engine.execute("delete from users where id=..."),但我想使用delete()方法.
谢谢!
你是如何在Flask中调试错误的?打印到控制台?Flash消息到页面?或者是否有更强大的选项可以找出出现问题时发生的情况?
我知道Flask有int,float和path转换器,但我们开发的应用程序在其URL中有更复杂的模式.
有没有办法可以使用正则表达式,就像Django一样?
例如,这个URL:
http://example.com/get_image?type=1
Run Code Online (Sandbox Code Playgroud)
应该返回image/gifMIME类型的响应.我有两个静态.gif图像,
如果type是1,它应该返回ok.gif,否则返回error.gif.如何在烧瓶中做到这一点?
我试图authorisation.py在包api中的蓝图内访问访问应用程序配置.我正在初始化__init__.py其中使用的蓝图authorisation.py.
__init__.py
from flask import Blueprint
api_blueprint = Blueprint("xxx.api", __name__, None)
from api import authorisation
Run Code Online (Sandbox Code Playgroud)
authorisation.py
from flask import request, jsonify, current_app
from ..oauth_adapter import OauthAdapter
from api import api_blueprint as api
client_id = current_app.config.get('CLIENT_ID')
client_secret = current_app.config.get('CLIENT_SECRET')
scope = current_app.config.get('SCOPE')
callback = current_app.config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
@api.route('/authorisation_url')
def authorisation_url():
url = auth.get_authorisation_url()
return str(url)
Run Code Online (Sandbox Code Playgroud)
我得到RuntimeError:在应用程序上下文之外工作
我明白为什么会这样,但是访问这些配置设置的正确方法是什么?
----更新----暂时,我已经这样做了.
@api.route('/authorisation_url')
def authorisation_url():
client_id, client_secret, scope, callback = config_helper.get_config()
auth = OauthAdapter(client_id, client_secret, …Run Code Online (Sandbox Code Playgroud) 我在使用Python查询文档上的聚合函数后从MongoDB返回的响应,它返回有效的响应,我可以打印它但不能返回它.
错误:
TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
打印:
{'result': [{'_id': ObjectId('51948e86c25f4b1d1c0d303c'), 'api_calls_with_key': 4, 'api_calls_per_day': 0.375, 'api_calls_total': 6, 'api_calls_without_key': 2}], 'ok': 1.0}
Run Code Online (Sandbox Code Playgroud)
但是当我试图返回时:
TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
它是RESTfull调用:
@appv1.route('/v1/analytics')
def get_api_analytics():
# get handle to collections in MongoDB
statistics = sldb.statistics
objectid = ObjectId("51948e86c25f4b1d1c0d303c")
analytics = statistics.aggregate([
{'$match': {'owner': objectid}},
{'$project': {'owner': "$owner",
'api_calls_with_key': {'$cond': [{'$eq': ["$apikey", None]}, 0, 1]},
'api_calls_without_key': {'$cond': [{'$ne': ["$apikey", None]}, 0, 1]}
}},
{'$group': {'_id': "$owner",
'api_calls_with_key': {'$sum': "$api_calls_with_key"},
'api_calls_without_key': {'$sum': "$api_calls_without_key"}
}}, …Run Code Online (Sandbox Code Playgroud) 是否有任何聪明的解决方案可以在Flask的应用程序根目录中存储静态文件.robots.txt和sitemap.xml应该在/中找到,所以我的想法是为它们创建路由:
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
response = make_response(open('sitemap.xml').read())
response.headers["Content-type"] = "text/plain"
return response
Run Code Online (Sandbox Code Playgroud)
必须有更方便的东西:)
我正在使用Flask-SQLAlchemy从用户数据库中进行查询; 然而,同时
user = models.User.query.filter_by(username="ganye").first()
Run Code Online (Sandbox Code Playgroud)
将返回
<User u'ganye'>
Run Code Online (Sandbox Code Playgroud)
干
user = models.User.query.filter_by(username="GANYE").first()
Run Code Online (Sandbox Code Playgroud)
回报
None
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法以不区分大小写的方式查询数据库,因此第二个示例仍将返回
<User u'ganye'>
Run Code Online (Sandbox Code Playgroud) 我的 Flask 应用服务器正在运行,但我有三个导入无法解析。

我努力了:
这是我的文件结构:
- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
- > app
- > venv
- config.py
- README.md
- requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt
Run Code Online (Sandbox Code Playgroud)
不幸的是,这些事情都没有解决我的进口问题,我的路线仍然无法正常工作。有什么想法/建议吗?