Translator App Project using Django
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:

Create the View
In main/views.py, add the following code:
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:
<!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:
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:
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



