Python Pyramid - HTML Form Template
Last Updated :
28 Apr, 2025
Pyramid is an open-source web application development framework written in Python. It is a flexible and modular framework that is used to build web applications from single-page to large, and complex websites. In this article, you will learn how to create the HTML Form in Python Pyramid. And How Pyramid reads data from HTML Form.
Setting up the project
Installation
Command to install a Pyramid
pip3 install pyramid
Command to install the Jinja2 template engine
pip install pyramid_jinja2
Creating Necessary files
Creating the HTML Form
First, we will create an HTML script as 'form.html' which will be used for rendering the Template object.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML Form</title>
</head>
<body>
<div class="container">
<form method="POST" action="http://localhost:5000/employees">
<p>Employee Id: <input type="text" name="id" required/> </p>
<p>Name: <input type="text" name="name" required/> </p>
<p>Department: <input type="text" name="department" required/> </p>
<p>Mobile No. <input type="number" name="mobile" required /></p>
<p>Salary: <input type="text" name="salary" required/> </p>
<p><input type="submit" value="Submit"> </p>
</form>
</div>
</body>
</html>
</html>
src.py
This code contain views to render the HTML Form and routes to define unique URL to each page.
- 'make_server' function is used to create a simple WSGI (Web Server Gateway Interface) server, which can be used to serve web applications.
- The Configurator class is used to configure a Pyramid application.
- The Response class is used to create HTTP responses that can be sent back to the client.
- The 'view_config' decorator is used to associate a view function with a specific route or URL pattern.
- The purpose of index function is to render an HTML form (defined in 'form.html') to collect user input. In this case, it returns an empty dictionary {} to the template so that it doesn't pass any specific data to be displayed on the form.
- The add function is associated with a route named 'employees' and uses the 'templates/marklist.html' template for rendering. It serves as the handler for the 'employees' route. The purpose of this function is to process the data submitted by the user through a form, typically via a POST request to the '/employees' URL.
- Inside the app function, it extracts data from the request's parameters (request.params) such as 'id', 'name', 'department', 'mobile No.' and 'salary'. It creates a dictionary employee with this extracted data then appends the employee dictionary to a list named employees. This suggests that this function is likely used to collect and store employee data.
- Finally, it returns a dictionary containing 'employees' as a key and the employees list as its value. This data will be passed to the 'marklist.html' template for rendering, where it may be used to display a list of students or their marks.
Python3
from wsgiref.simple_server import make_server
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.config import Configurator
employees = [
{"id": 1, "name": "Sameer","department":"Sales",
"mobile":6756453456, "salary": 75000},
{"id": 2, "name": "Rahul","department":"Content",
"mobile":8978675678, "salary": 50000},
{"id": 3, "name": "Mahesh","department":"Marketting",
"mobile":7989881110, "salary": 30000},
]
@view_config(route_name='index', renderer='form.html')
def index(request):
return {}
@view_config(route_name='employees', renderer='marklist.html')
def add(request):
employee={'id':request.params['id'], 'name':request.params['name'],
'department':request.params['department'],
'mobile':request.params['mobile'],
'salary':int(request.params['salary'])}
employees.append(employee)
return {'employees':employees}
if __name__ == '__main__':
with Configurator() as config:
config.include('pyramid_jinja2')
config.add_jinja2_renderer(".html")
config.add_route('index', '/')
config.add_route('employees','/employees')
config.scan()
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 5000, app)
server.serve_forever()
marklist.html
This 'marklist.html' web template displays a table of employee data along with the computed result column.
HTML
<html>
<head>
<title>Employee List</title>
</head>
<body>
<table border=1>
<thead>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Department</th>
<th>Mobile No.</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
{% for Employee in employees %}
<tr>
<td>{{ Employee.id }}</td>
<td>{{ Employee.name }}</td>
<td>{{ Employee.department }}</td>
<td>{{ Employee.mobile }}</td>
<td>{{ Employee.salary }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="/" style="text-decoration: none;"><button>Back</button></a>
</body>
</html>
Deployment
Run the 'src.py' file in the terminal. In your browser, visit http://localhost:5000/ to get the form as shown below −
Output:

Fill the employee data and click the submit button -

After submitting the form, it invokes add view and display the list of employees when you click 'Back' button it will redirect you to form page-

Similar Reads
Python Pyramid - Templates Templates are a crucial part of any web application, enabling developers to create dynamic HTML content by embedding Python code within HTML. Pyramid supports several templating engines, including Jinja2, Chameleon, and Mako. Each of these engines has its own syntax and features, but they all serve
4 min read
How to use HTML in Tkinter - Python? Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to
2 min read
Placeholders in jinja2 Template - Python Web pages use HTML for the things that users see or interact with. But how do we show things from an external source or a controlling programming language like Python? To achieve this templating engine like Jinja2 is used. Jinja2 is a templating engine in which placeholders in the template allow wri
5 min read
Email Templates with Jinja in Python HTML emails play a crucial role in modern communication, whether for newsletters, notifications, or transactional emails. Jinja, a powerful templating engine for Python, offers flexibility for creating dynamic and visually appealing HTML email templates. In this article, we will see how we can use J
3 min read
Switching From Other Template in Python Jinja Jinja, a template engine, seamlessly combines the power of Python with HTML's structure. It enhances the development experience, making web applications more flexible. Jinja solves the challenge of cleanly integrating server-side logic (Python) with client-side presentation (HTML). It eliminates clu
4 min read