Sphinx Autoflask为同一函数上的GET和POST请求区分DocString

2024-09-29 02:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Flask应用程序,我正在通过pythonsphinx构建文档。在

我当前正在使用来自sphinxcontrib.autohttp.flask的autoflask扩展

我的问题是:如何准备一个docstring,它将不同的信息正确地应用于同一路由的GET版本和POST版本。在

例如一个小函数:

@app.route('/add_event', methods=['GET', 'POST'])
def add_event():
    """
    ..http:get:: /add_event
        :return: Test
    ..http:post:: /add_event
        :return: Test2
    """
    if request.method == 'GET':
        # get some things
        person_id = request.args.get('id')
        return render_template('create_event.html', race_event_form=test_form)
    if request.method == 'POST':
        # post some things

        return redirect('/person_profile/id/{0}'.format(request.args.get('id')))

我当前的扩展配置文件在

^{pr2}$

斯芬克斯的输出看起来像

enter image description here

有没有可能对一件事做一件事,另一件事做一个职位?我真的希望避免将每个函数拆分为两个单独的get/post函数。在

另外,是否可以通过request.argsrequest.form传递autoflask output所需的参数,就像GET请求中的person_id变量一样?在


Tags: 函数版本formeventaddidgetreturn
1条回答
网友
1楼 · 发布于 2024-09-29 02:26:41

尝试从Flask autodoc中删除重载的view函数,并单独定义请求,同时让API的其余部分自动生成。在

这里有个例子。。。在

conf.py(插件的顺序是重要的):

extensions = [
    'sphinx.ext.autodoc',
    'sphinxcontrib.httpdomain',
    'sphinxcontrib.autohttp.flask',
]

在RestructedText文件中:

^{pr2}$

关于第二个问题,您可以查看httpdomainhere的指令。在

相关问题 更多 >