Submit Form in Flask

Hello World, Today we are going to discuss that how we can submit form in flask with both get and post method.Before reading this tutorial, i will request you to read my previous article that create hello world app with flask.In this tutorial i will submit form with both get and post method. I have divided this tutorial in following parts:

  • Create basic structure of app for this tutorial
  • Create form template
  • Process the form according to submitted data

Create basic structure of app for this tutorial

In this tutorial, i will read RSS feed from different news websites. It will work when user submit the name of the news channel than data will come from that website. We will perform this action with both GET and POST method. We will also use frontend template for this tutorial. By default flask comes with jinja template engine. We will use this template. So lets start the work

  • Create news folder.
  • Create in this folder.
  • Create templates folder in news folder with news.html file.

We have created the file structure of our app. Now we will put the code these files.

Create Form Template

In this step, we will create the form and submit the form with flask.

  • Open news.html and add the following code in it.
<!DOCTYPE html>
	<title>News Application</title>
	<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='bootstrap.min.css')}}" />
	<div class="container">
		<div class="page-header">
			<!-- <form method="post"> -->
				<div class="row">
					<div class="col-xs-10">
						<select name="search" class="form-control">
							<option value="">--- Select News Channel ---</option>
							<option value="bbc">BBC</option>
							<option value="cnn">CNN</option>
							<option value="ndtv">NDTV</option>
							<option value="rediff">Rediff</option>
					<div class="col-xs-2">
						<input type="submit" class="btn btn-danger btn-block" value="Submit" />
		{% for news in articles %}
		<div class="panel panel-default">
		  <div class="panel-heading">
		    <h3 class="panel-title"><a href="{{}}">{{news.title}}</a></h3>
		  <div class="panel-body">
		  <div class="panel-footer">
		    <h3 class="panel-title">{{news.published}}</h3>
		{% endfor %}

Process the form according to submitted data

  • Open and add the following code.
from flask import Flask
from flask import render_template,request
import feedparser
def home():
	# query=request.form.get('search') #Enable Post Method
	query=request.args.get('search') #Enable GET Method
	if not query or query.lower() not in NEWS_CHANNEL: 
	return render_template('news.html',articles=news)

if __name__=='__main__':

The above code process the user submitted data and show the relative result. I have commented the get method in the above code. If you want to run your application with get method then remove the comment from the line and comment the second method. Also change the form method form template. Now its time the run the application. Run this application with following steps.

  • Go to news directory via command prompt or terminal.
  • Run this command python
  • Open url at given port in browser.
  • You will see the following screen in the browser.


  • When you submit the form with selected channel than you will get the latest news of that channel.
  • Make sure that your internet connection is working because data is coming from live websites.

In this tutorial, I have Bootstrap 3 for styling the template.

Congratulations, We have created the form and submit that form with flask.

Leave a Reply

Your email address will not be published. Required fields are marked *