Python Compiler Using Django
Last Updated :
24 Apr, 2025
In this article, we will explore the creation of a Python compiler using Django. Users can input their Python code into the designated code area, and upon clicking the "Run" button, the compiler will generate the corresponding output.
What is Python Compiler?
A Python compiler is a program or tool that translates human-readable Python code into machine-readable bytecode or native machine code. Unlike an interpreter that executes code line by line, a compiler processes the entire code at once, generating an intermediate representation or executable file. Python compilers play a crucial role in transforming high-level Python code into a format that a computer's hardware can execute directly, making the code run.
Create Python Compiler Using Django
Below, is the step-by-step guide to creating a Python Compiler Using Django:
Starting the Project Folder
To start the project use this command
django-admin startproject core
cd core
To start the app use this command
python manage.py startapp home
Now add this app to the ‘settings.py’
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"home",
]
File Structure

Setting Necessary Files
views.py : below,code defines a Django view for executing Python code submitted through a web form. It uses `exec` to run the code, captures the output, and handles exceptions, providing detailed error information if any. The `index` function renders an HTML template, while `runcode` processes a POST request containing Python code, executes it, and updates the web page with the code and its output.
Python3
# views.py
from django.shortcuts import render
import traceback
from io import StringIO
from contextlib import redirect_stdout # Add this import
def execute_code(code):
try:
# Capture standard output in a buffer
output_buffer = StringIO()
with redirect_stdout(output_buffer):
exec(code)
output = output_buffer.getvalue()
except Exception as e:
# Provide detailed error information
output = f"Error: {str(e)}\n{traceback.format_exc()}"
return output
def index(request):
return render(request, 'index.html')
def runcode(request):
if request.method == "POST":
codeareadata = request.POST['codearea']
output = execute_code(codeareadata)
return render(request, 'index.html', {"code": codeareadata, "output": output})
return HttpResponse("Method not allowed", status=405)
core/urls.py : below code sets up URL patterns for a Django project. It includes the admin interface at '/admin/' and incorporates URLs from the 'home' app at the project's root ('/').
Python3
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
]
home/urls.py : below Django URL configuration defines two patterns. The empty path maps to the 'index' view, identified as 'indexpage'. The 'runcode' path links to the 'runcode' view.
Python3
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name="indexpage"),
path('runcode', views.runcode, name="runcode"),
]
Creating GUI
home/templates/index.html : This HTML file creates a straightforward Python compiler web page with Django. It features a CodeMirror code area, a 'Run Code' button, and an output section. The design is clean with a green color scheme and a GeeksforGeeks-themed header. The button triggers code execution, and the output is displayed below the input area. The page maintains a responsive layout.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>GeeksforGeeks</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/codemirror.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/mode/python/python.min.js"></script>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f0f4f7;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
form {
width: 100%;
max-width: 800px;
background-color: #ffffff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
}
label {
display: block;
margin-bottom: 12px;
font-weight: bold;
color: black;
font-family: 'Times New Roman', Times, serif;
}
#codearea,
#output {
height: 300px;
font-size: 16px;
width: 100%;
border: 1px solid green;
border-radius: 5px;
padding: 12px;
margin-bottom: 24px;
resize: vertical;
}
#output {
height: 150px;
padding: 5px;
}
button {
color: green;
border: none;
padding: 10px 20px;
border-radius: 5px;
border: 1px solid green;
cursor: pointer;
font-size: 15px;
}
button:hover {
background-color: #388e3c;
color: white;
}
</style>
</head>
<body>
<h2 style="font-family: 'Times New Roman', Times, serif; "> <strong style="color: green;">GeeksforGeeks</strong> Python Compiler</h2>
<form action="/runcode" method="post">
{% csrf_token %}
<div>
<label for="codearea">Code Area</label>
<textarea id="codearea" name="codearea" rows="10" >{{ code }}</textarea>
</div>
<hr>
<div>
<label for="output">Output</label>
<textarea id="output" name="output" rows="4" disabled>{{ output }}</textarea>
</div>
<button type="submit">Run Code</button>
</form>
<script>
var codeEditor = CodeMirror.fromTextArea(document.getElementById("codearea"), {
mode: "python",
lineNumbers: true,
indentUnit: 4,
extraKeys: { Tab: "indentMore", "Shift-Tab": "indentLess" },
});
</script>
</body>
</html>
Deployment of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output

Similar Reads
Calculate CGPA using Python Django
This article demonstrates how to build a CGPA calculator and display the results using Django. Initially, we establish a login system to ensure security. Users must first register for an account and then log in to access the calculator. After logging in, users can input their subject names, CGPA, an
11 min read
Weather app using Django | Python
Our task is to create a Weather app using Django that lets users enter a city name and view current weather details like temperature, humidity, and pressure. We will build this by setting up a Django project, creating a view to fetch data from the OpenWeatherMap API, and designing a simple template
2 min read
Complete Django History | Python
Prerequisite - When to Use Django ? Comparison with other Development Stacks Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle. Django was rel
2 min read
Job Board using Django
In this article, we will guide you through creating a Job Board using Django in Python. Job Board Using DjangoBelow, is the step-by-step Implementation of a language learning app using Django in Python: Starting the Project FolderTo start the project use this command django-admin startproject Job_Bo
3 min read
Blogging Platform using Django
Our task is to build a simple blogging platform using Django. We will learn how to create, display, edit, and delete blog posts with images using Djangoâs models, views, templates, and admin panel. Step-by-step, weâll set up the project, connect it to a database, and run it locally.Project SetupPrer
4 min read
Online Resume Builder using Django
In this article, we will create an online resume builder web app. We will start by filling out a form with our personal information. Once the form is completed, we can generate a resume. Additionally, we will ensure the PDF generation quality to make the final result closely resemble the original re
9 min read
E-commerce Website using Django
This project deals with developing a Virtual website âE-commerce Websiteâ. It provides the user with a list of the various products available for purchase in the store. For the convenience of online shopping, a shopping cart is provided to the user. After the selection of the goods, it is sent for t
11 min read
E-commerce Product Catalog using Django
We will build a simple e-commerce product catalog using Django. You'll learn how to create a product model, display product listings and details with dynamic templates, handle images, and configure URLs and media files all step-by-step to help you create a solid foundation for an online store.Create
4 min read
Python Web Development With Django
Python Django is a web framework that allows to quickly create efficient web pages. Django is also called batteries included framework because it provides built-in features such as Django Admin Interface, default database - SQLite3, etc. When youâre building a website, you always need a similar set
15+ min read
Note-taking App using Django
In this article, we will explore a note-making app. In this article, we will create a note-making app using Django. We will also use the Django authentication system. Users need to create an account on the web to access the note-making app. After that, users will have access to the note-making app.
8 min read