How to create a Dictionary application in Django

In this Python Django tutorial, we will learn how to create a Dictionary app in Django. We will use a module of Python to generate dictionary pairs related to our search. Where you will enter a word and it will generate the meaning of that word and its type of Parts of Speech.

We will also generate antonyms and synonyms of the word which we will search through our dictionary.

How to create a Dictionary application in Django?

Here we will develop this application using Python Django.

  • Firstly, set up a Django Project with the basic configurations.
  • Import the os module and install PyDictionary
  • Use templates to create our home page
  • Use bootstrap and CSS to make our application look robust.

To create a dictionary application in Python Django First set up a Django project with the virtual environment in a folder or directory in which you will set up your Django project.

Here I have created a Django Project djangodictionary and application dictionary. Now, register the application in the INSTALLED APP section of the setiings.py file.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dictionary',
]

Install PyDictionary

PyDictionary is a Dictionary Module for Python that generates translations, synonyms, and Antonyms of words. It uses WordNet for getting meanings, Google for translations, and synonym.com for getting synonyms and antonyms.

Follow the below command in the terminal to install PyDictionary

pip install PyDictionary

In case you get any error in installing the PyDictionary first run the below code in the terminal and then install PyDictionary.

pip install -e git+https://github.com/yeahwhat-mc/goslate#egg=goslate

Read Django User Registration with Email Confirmation

Creating templates

Create templates home.html and word.html in the dictionary app, where we will render our home page with home.html and word.html, which will generate search results for our dictionary. Follow the below code for both HTML files.

READ:  Django CRUD Example with PostgreSQL

home.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>Django Dictionary</title>
    <style>
        body{

            background: rgb(32, 142, 232);

            color: #fff;
            max-width: 900px;
            min-height: 100vh;
            margin: 0 auto;
            padding-top: 100px;


        }
        h1{
            color: black;
            text-align: center;
            font-family: serif;
        }
        /* .container {
            max-width: 550px;
            margin: auto;
            background-color: #fff;
            -webkit-box-shadow: 2px 2px 13px -4px rgba(0, 0, 0, 0.21);
            box-shadow: 2px 2px 13px -4px rgba(0, 0, 0, 0.21);
        } */
        .card-header{
            background: cyan;

        }
        .btn{
            background: rgb(107, 187, 237);
            color: rgb(47, 0, 29);
            text-transform: uppercase;
        }
        .btn:hover{
            background: blueviolet;


        }
        a{
            color: rgb(31, 0, 23);
            text-decoration: none;
        }
        a:hover{
            color: rgb(0, 0, 0);
        }
        svg{
            width: 40px;
            height: 40px;
        }
    </style>
  </head>
  <body>
    <h1><a href="word.html">
        <h1><a>Django Dictionary</a></h1>
       
    <div class="container">
        <div class="card text-center">
            <div class="card-header">
              <ul class="nav nav-tabs card-header-tabs">
                <li class="nav-item" style="color: white;">
                  <a class="nav-link active" aria-current="true" href="#">Dictionary</a>
                </li>
                <!-- <li class="nav-item">
                  <a class="nav-link" href="#" style="color: white;">History</a>
                </li>
                <li class="nav-item" style="color: white;">
                  <a style="color: white;" class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Login</a>
                </li> -->
              </ul>
            </div>
            <div class="card-body">
              <h5 class="card-title"></h5>
              <form action="word">
                <input type="text" name="search" class="form-control" id="exampleInputEmail1"  placeholder="Enter a word to search">
                <br> <button class="btn" type="submit">Search</button>
              </form>
            </div>
          </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
  </body>
</html>

for word.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>Django Dictionary</title>
    <style>
        body{

            background:  rgb(32, 142, 232);

            color: #fff;
            max-width: 900px;
            min-height: 100vh;
            margin: 0 auto;
            padding-top: 100px;


        }
        h1{
            color: black;
            text-align: center;
            font-family: serif;
        }
        /* .container {
            max-width: 550px;
            margin: auto;
            background-color: #fff;
            -webkit-box-shadow: 2px 2px 13px -4px rgba(0, 0, 0, 0.21);
            box-shadow: 2px 2px 13px -4px rgba(0, 0, 0, 0.21);
        } */
        .card-header{
            background: cyan;

        }
        .btn{
            background: rgb(107, 187, 237);
            color: rgb(47, 0, 29);
            text-transform: uppercase;
        }
        .btn:hover{
            background: blueviolet;


        }
        #sh2{
            text-align: left;
            color: rgba(51, 51, 51, 0.808);
            font-size: 20px;
            padding-top: 20px;
        }
        #fh2{
            color: black;
            font-size: 25px;
            font-family: Arial, Helvetica, sans-serif;
        }

        a{
            color: rgb(31, 0, 23);
            text-decoration: none;
        }
        a:hover{
            color: rgb(0, 0, 0);
        }
        svg{
            width: 40px;
            height: 40px;
        }
    </style>
  </head>
  <body>
    <h1><a href="home.html">
        <h1><a>Django Dictionary</a></h1>
        
    <div class="container">
        <div class="card text-center">
            <div class="card-header">
              <ul class="nav nav-tabs card-header-tabs">
                <li class="nav-item" style="color: white;">
                  <a class="nav-link active" aria-current="true" href="#">Dictionary</a>
                </li>
                <!-- <li class="nav-item">
                  <a class="nav-link" href="#" style="color: white;">History</a>
                </li>
                <li class="nav-item" style="color: white;">
                  <a style="color: white;" class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Login</a>
                </li> -->
              </ul>
            </div>
            <div class="card-body">
              <h5 class="card-title"></h5>
              <form action="word">
                <input type="text" name="search" class="form-control" id="exampleInputEmail1"  placeholder="Enter a word to search">
                <br> <button class="btn" type="submit">Search</button>
              </form>
            </div>
          </div>

          <h1 id="sh2" >Search Results</h1> <br>
          <h2 id="fh2">Meaning: {{ meaning }}</h2> <br>

          <h2 id="fh2">Synonyms: {% for synonym in synonyms %}
                                {{ synonyms }},
                                {% endfor %}
          </h2> <br>

          <!-- <h2 id="fh2">Antonyms: {% for antonym in antonyms %}{{ antonyms }}, {% endfor %}</h2> <br> -->

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
  </body>
</html>

Register templates and import os module

READ:  Payment Gateway Integration with Django

Now we are going to register our templates in the settings.py file. First import the os module in the settings.py file

Now, go to the templates section in settings.py and register templates in the DIR

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,

Create URLs in the dictionary app

from django.urls import path
from . import views

urlpatterns =[
    path('', views.home),
    path('word', views.word)
]

Create URLs in the project

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

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

Create views in the app

from django.shortcuts import render
from PyDictionary import PyDictionary

def home(request):
    return render(request, 'home.html')

def word(request):
    search = request.GET.get('search')
    dictionary = PyDictionary()
    meaning = dictionary.meaning(search)
    synonyms= dictionary.synonym(search)

    context ={
        'meaning':meaning,
        'synonyms':synonyms,
    }
    return render(request, 'word.html',context)

Run the development server

We are done with all the necessary configurations of our application and now we are ready to run our app on the server where we can view the app. To run the development server we will use the runserver command.

python manage.py runserver

Now the URL link  http://127.0.0.1:8000/ page will redirect you to the server where we will see our application.

Python Django Dictionary application
Dictionary application in Django

Conclusion

In this Python Django tutorial, we came to know about the os module and its functionalities in Python. We also learned about the PyDictionaries and with the help of that, we have successfully created this Dictionary application in Python using templates.

Along with word meaning, we also added antonyms, synonyms, and parts of speech to the result of our dictionary search.

We have also used Bootstrap and CSS and added them to our templates to make our application look more dynamic.

READ:  Python Django get - Everything you need to know

You may also like to read: