Open In App

Translator App Project using Django

Last Updated : 16 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

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

In this article we will make a translator app using Django.

Installation

pip install django

Text translation from one language to another is increasingly becoming common for various websites as they cater to an international audience. The python package which helps us do this is called translate.

pip install translate

Then create new project

django-admin startproject translator
cd translator

Then create new app inside the project

python manage.py startapp main

Then add the app name inside the settings.py 

views.py

Python3




from django.shortcuts import render,HttpResponse
from translate import Translator
# Create your views here.
  
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")


Then create directory templates inside the app.

Inside that create new directory main

Create new file index.html

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>


Then create new file urls.py inside the app

Python3




from django.urls import path
from .views import *
  
urlpatterns = [
    path('',home,name="home"),
]


Then add then main.urls inside url translator/urls.py

Python3




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


To run this app open cmd or terminal 

python manage.py runserver

Output :-



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads