问题标签 [flask-restful]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
233 浏览

caching - 充分利用缓存

我正在缓存一个方法的结果(显然带有它的签名),这样它就不会每次都对我的数据存储进行复杂的查询。我的缓存工作正常。

我的问题是:

我应该如何找到缓存中条目的最佳超时值?

缓存中的最佳条目数应该是多少?

我可以更改它们以提高应用程序的性能吗?

将影响缓存性能的各种因素假设为变量,并为我提供一个公式来帮助理解如何优化我的缓存?

0 投票
1 回答
10079 浏览

flask - 使用 Flask-Restful 引发自定义错误

所有,我正在尝试使用 Flask-Restful 引发自定义错误,遵循docs。出于测试目的,我在文档中定义并注册了错误字典的确切链接:api = flask_restful.Api(app, errors=errors).

但是,当我想abort(409)在资源模块中使用(例如)引发自定义错误时,萤火虫报告:

{“消息”:“冲突”,“状态”:409 }

这似乎是标准的 409 错误,没有自定义;从文档中,我希望自定义错误消息 - “具有该用户名的用户已经存在。”

我想我遗漏了一些关于错误本身的提升。我应该以任何方式使用字典键吗?尽管我尝试过,但查看 Flask-Restful 源代码并没有帮助。

0 投票
1 回答
5529 浏览

python - 如何在 Flask 中设置不同的子域(使用蓝图)?

我有一个在https://app.mydomain.com运行的 Flask 应用程序。

蓝图如下所示:

URL 如下所示:

我想将api_1_0路由从https://app.mydomain.com/api/v1.0移动到https://api.mydomain.com,我应该如何修改路由以及应该如何设置app.config['SERVER_NAME']

example.com(没有任何子域)完全是另一个站点,否则我会摆脱app子域。

所以,我想app成为所有蓝图的默认子域,除了api_1_0应该是api.

0 投票
2 回答
23841 浏览

python - Flask - 在 after_request 或 teardown_request 中访问请求

我希望能够在返回 HTTP 调用的响应之前访问请求对象。我想通过“teardown_request”和“after_request”访问请求:

我看到我可以将请求添加到 g 并执行以下操作:

但是上面的似乎有点奇怪。我确信有更好的方法来访问请求。

谢谢

0 投票
1 回答
500 浏览

python - 如何在 gae 中传递/模拟 admin_required 装饰器?

如果我在视图中添加 @admin_required 装饰器,则单元测试开始失败,并显示以下消息:

RuntimeError:在请求上下文之外工作

有没有办法模拟它或绕过它进行单元测试?

这是装饰器:

0 投票
2 回答
2013 浏览

python - Generating url with fields.Url when using Flask-Restful generates BuildError

I wanted to adapt the wonderful Tutorial from Miguel Grinberg to create a unittest test-executor. I principally just adapted the code from Miguel to my needs, but I get a problem with generating the uri inside the fields map. As long as I remove the

everything works fine, but otherwise I get a Build Error:

I discovered very similar Question here on stackoverflow here but this did not help me to understand what is wrong with my code.I can use the described work-around from that question, but I would really like to know what am I doing wrong.

Here is my code:

0 投票
1 回答
5053 浏览

python - python中flask rest api中的错误404

我是flask Restful api的新手,我正在尝试制作一个获取JSON数据的rest api。输入curl命令时出现404错误。这是任何人都可以帮忙的代码。

显示的错误是:

生成的日志是:

0 投票
1 回答
812 浏览

flask - flask wtforms_alchemy object has no attribute

I just started doing flask restful api and trying to register a user by sending a POST request to localhost:5000/api/v1/users. The required fields are email and password. Here's the curl request to create a user curl http://localhost:5000/api/v1/users -d "email=admin@example.com&password=password" -X POST -v

But it returns me this error: AttributeError: 'UserCreateForm' object has no attribute 'password'

Below is some snip of my code

views.py

forms.py

models.py ( I tired to place the UserMixin to second, still getting same error )

Also, I have login_manager.user_loader included, so I don't think it will be issue with flask login.

And is there a documentation for wtform_alchemy? I totaly don't get how it works to create the form automatically from the models.

0 投票
2 回答
1380 浏览

python - Flask Restful:根据 URL 参数更改表示

我正在使用 Flask 和 Flask-Restful 构建一个 API。API 可能由不同类型的工具(Web 应用程序、自动化工具等)访问,其中一项要求是提供不同的表示形式(为了示例,假设为 json 和 csv)

正如在 restful 文档中所解释的,很容易根据内容类型更改序列化,所以对于我的 CSV 序列化,我添加了这个:

它在使用 curl 并传递正确的-H "Accept: text/csv"参数时起作用。

问题是,由于某些浏览器可能会直接路由到 url 以下载 csv 文件,因此我希望能够通过 url 参数强制我的序列化,例如http://my.domain.net/api/resource?format=csv,它format=csv的效果与-H "Accept: text/csv".

我已经阅读了 Flask 和 Flask-Restful 文档,但我不知道如何正确处理这个问题。

0 投票
1 回答
2371 浏览

python - Flask 和 Apache 的 API 错误 500

我正在尝试让我的 API 使用 Apache 在我的 Ubuntu VPS 上工作,这是我的 FLASK 代码;

每次我尝试运行 Curl 命令时;

它抛出;

我不明白为什么会发生这种情况,因为它可以在我的本地计算机上完美运行,但是如果我尝试从 VPS 运行它,它只会抛出错误 500。

我在网上搜索过,但似乎找不到解决方案。

非常感谢所有帮助。

添加;

这是错误;

我说; 从 FlaskApi 导入应用程序

但是wsgi和init文件在同一个地方,是这个问题吗>>?