简体   繁体   English

使用Flask发送和接收GET请求时出现问题

[英]Issues sending and receiving a GET request using Flask

I'm having issues with correctly sending and receiving a variable with a GET request. 我在正确发送和接收带有GET请求的变量时遇到问题。 I cannot find any information online either. 我也无法在线找到任何信息。 From the HTML form below, you can see I'm sending the value of 'question' but I'm also receiving 'topic' from a radio button in the form (though the code is for that is not below). 从下面的HTML表单中,您可以看到我正在发送“问题”的值,但是我还从表单中的单选按钮接收了“主题”(尽管该代码不在下面)。

I want to send 'topic' using POST but use GET for 'question'. 我想使用POST发送“主题”,但将GET用于“问题”。 I'm aware that the form method is POST though I'm not sure how to cater for both POST and GET. 我知道form方法是POST,尽管我不确定如何兼顾POST和GET。

HTML Form: HTML表单:

<form method="POST" action="{{ url_for('topic', question=1) }}">

My second issue is that I'm unsure how to receive 'topic' AND 'question' from the form. 我的第二个问题是我不确定如何从表单中接收“主题”和“问题”。 I've managed to receive 'topic' as seen below but I'm not quite sure how to receive 'question'. 我设法收到了如下所示的“主题”,但是我不太确定如何接收“问题”。 Preferably it would be better for the URL to be like so: 最好让URL像这样更好:

www.website.com/topic/SomeTopic?question=1 www.website.com/topic/SomeTopic?question=1

For the code below, I found online that request.args[] is used for receiving GET requests though I'm not sure if it is correct. 对于下面的代码,我在网上发现request.args []用于接收GET请求,尽管我不确定它是否正确。

Flask: 烧瓶:

@app.route('/topic/<topic>', methods=['POST', 'GET'])
def questions(topic):
    question = request.args['questions']
    return render_template('page.html')

The question is 问题是

  1. How do I send two variables from a form using GET and POST for different variables at the same time. 如何同时使用GET和POST从表单发送两个变量以获取不同的变量。
  2. How would I go about receiving both variables? 如何接收两个变量?

The short answer to your question is that you can't send both GET and POST using the same form. 这个问题的简短答案是,您不能使用相同的表单发送GET和POST消息。

But if you want your url to look like you specified: 但是,如果您希望自己的网址看起来像您指定的网址:

www.website.com/topic/SomeTopic?question=1

then you're almost there. 那你就快到了。 First you will need to already know the name of the topic as you have to specify that in your call to url_for() for the questions url. 首先,您将需要知道主题的名称,因为您必须在对问题url的url_for()调用中指定该主题的名称。

<form method="GET" action="{{ url_for('questions', topic_name="cars") }}">
# Your url will be generated as www.website.com/topic/cars

flask 烧瓶

# Note that I changed the variable name here so you can see how
# its related to what's passed into url_for
@app.route('/topic/<topic_name>')
def questions(topic_name):
    question = request.args['question']
    return render_template('page.html')

Now when you submit your form, your input will be sent as a GET, an asumming you have an input field with the name question you'll be able to get the value of that field. 现在,当您提交表单时,您的输入将以GET的形式发送,假设您有一个带有名称question的输入字段,便可以获取该字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM