Open In App

Translator App Project using Django

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We will build a simple translator app using Django. We will learn how to set up a project, create a form to input text and select a language, and use the Python translate package to convert text from one language to another. Step-by-step, we’ll create a working app that translates user input into different languages.

Install Required Packages

Open your terminal and run:

pip install django
pip install translate

Create Django Project and App

Refer to the following articles to check how to create a project and an app in Django.

Open terminal and run:

django-admin startproject translator
cd translator
python manage.py startapp main

Add App to Installed Apps

In translator/settings.py, add "main" to the INSTALLED_APPS list:

InstalledAppsInSettings

Create the View

In main/views.py, add the following code:

Python
from django.shortcuts import render,HttpResponse
from translate import Translator

def home(request):
    if request.method == "POST":
        text = request.POST["translate"]
        language = request.POST["language"]
        translator= Translator(to_lang=language)
        translation = translator.translate(text)
        return HttpResponse(translation)
    return render(request,"main/index.html")

Create Templates Directory and HTML File

Inside the main app folder, create these directories:

main/
└── templates/
└── main/
└── index.html

Create index.html with the following content:

HTML
<!DOCTYPE html>
<html>
<head>
    <title>GFG</title>
</head>
<body>
<form method="post">
    {% csrf_token %}
    <input type="text" name="translate" required>
    <br>
    <select required name="language">
        <option value="Hindi">Hindi</option>
        <option value="Marathi">Marathi</option>
        <option value="German">German</option>
    </select>
    <br>
    <button type="submit">Translate</button>
</form>
</body>
</html>

Configure URLs in the App

Create main/urls.py:

Python
from django.urls import path
from .views import *

urlpatterns = [
    path('',home,name="home"),
]

Include App URLs in Project URLs

In translator/urls.py, include the app’s URLs:

Python
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include("main.urls"))
]

Run the Development Server

Run the Django server:

python manage.py runserver

Output


Next Article

Similar Reads