03-29-2023, 04:46 AM
How I Built an English Dictionary App with Django
5/5 – (1 vote)
As we all know, Django is a Python framework used for building complex web applications. Yet, it can also be used for building simple ones. In this tutorial, I’m going to show you
how I built an English dictionary app, a simple Django project for beginners.
It takes time for a beginner to fully understand how Django works. But the key is consistency. If you keep building projects, you will gradually know enough to be dangerous with Django.
Scroll to the end to see the full code on GitHub!
A Dictionary App?![[Image: 1f4d6.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4d6.png)
![[Image: image-408.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-408.png)
A dictionary is an application used to get a particular word’s meaning. No matter how proficient we are in English, some words are far beyond our understanding. There are also words we may not know how to pronounce efficiently, especially those originating from a different language.
Furthermore, we may want to know a synonym or antonym of a given word. This is where a dictionary comes in handy. With a dictionary, we get to know not only the meaning of a word but how it is pronounced, what parts of speech it belongs to, and how it is used in a sentence. Students, no doubt, appreciate the importance of having a dictionary.
How Will We Get The Data?
How will we build this dictionary app so that when users search for a particular word, they get the meaning?
We have two options. Either we use, a Python module and an API that scrapes an online dictionary website or we perform the web scraping ourselves. None of these methods comes without a drawback.
Recommended: Is Web Scraping Legal?
The drawback is the challenge that comes with web scraping. Many websites sometimes change their HTML structures to prevent people or bots from accessing their content. So, if an API is not actively maintained, it can quickly become useless. This is the case with. Therefore, we are going to perform web scraping.
Doing so will undoubtedly gain some valuable experience in web scraping, a very broad concept in Python programming. Still, we are at the mercy of website owners. They can change the HTML structures of their websites unexpectedly.
Prerequisites
![[Image: image-407.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-407.png)
This tutorial assumes you are familiar with Python, HTML, and Bootstrap. If not, please you are required to have at least a basic knowledge before attempting this project. Basic knowledge of Django, though not required, can speed up the learning process.
Getting started
Create a new folder for this project. Then you do the following:
Creating Django Project and Application
i. Creating the project
This creates a folder called in the current directory.
ii. Creating the application
The file is an executable file used to run Django commands
iii. Applying migrations
iv. Checking to see if the installation was successful
![[Image: image-400.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-400.png)
Once you see the above image, you are good to go.
Registering the App
![[Image: image-406.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-406.png)
We have registered the app in the file. The file is used to configure the whole project.
We have registered the app-level URL. Let’s now create it.
Creating the App URL
This will be done inside the dictionary folder. Create a file inside the folder and add this:
“” tells Django to import the file residing in the dictionary folder.
The empty string indicates the home page URL which is if you are using the local server.
This is where users will be directed when the web page is opened.
For the string, the URL will be . We will be creating two functions in the file as indicated.
Notice the argument in the function, this is another way to refer to the URLs in the template files without using the exact URLs.
Creating Views
![[Image: image-405.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-405.png)
In this file, we are going to perform the web scraping we mentioned earlier. The first function, the is fairly simple. We just render the page once the app is opened.
But the function is a little bit complex. We want to display three things when a user searches for the meaning of a word: meaning, synonyms, and antonyms.
So, we perform web scraping on two online dictionary websites. The first will display the meaning and the second will display the synonyms and antonyms.
We import the module which uses the method to scrape the contents of the two websites.
Recommended: My First Python Get Request
The result is a cruel HTML code staring at you. BeautifulSoup, invaluable for web scraping does its job well to pull the data out of the HTML content. This now makes it possible to search the parsed data.
For our first search using the method from , we want a text wrapped in a div tag with the value 1. This is where the website owners kept the meaning of a particular word on the dictionary.com website.
So, we use the method to get the text.
The second and third searches look similar to the first but instead of a div, we search in a tag. The words were kept in a tag to make it easy for visitors to be redirected to that particular word if they want to find the definition.
Instead of searching for a value, we search for a CSS class. We then append the results to their respective variables. If you are new to web scraping, you may wonder how we know where those particular words reside.
The answer is found in a browser’s developer tools, preferably Chrome Developer Tools. This is the reason I suggest familiarizing yourself with HTML before attempting this project.
Finally, we render the results on the page. Notice the statements. This is run if the word is not found on the website. Let’s now create the HTML files.
Creating Templates
![[Image: image-404.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-404.png)
Create a folder named in the current directory (not in the project or app folder). Then go to the file to let Django know of this development. In the file, import the module. Then scroll to the TEMPLATES section and add this:
Then create three files in the folder:
The main HTML files will inherit from the file. Open the file and add the following:
Then, in the add the following:
Recall in the function, we send a GET request to get the word input by our user from a form that bears the name Can you now see it in the form?
There will be a problem if you use it otherwise. Finally, add the following to the file.
We use Django templating language to render the results of the function to the HTML. Alright, we are done. Run the server once again and you should see something similar to the images below.
![[Image: image-401.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-401.png)
That is the web page. Once you search for a word, the page is displayed as you can see below.
![[Image: image-402.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-402.png)
Do you see how each of the pages inherits everything in the file?
Conclusion
![[Image: image-403.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-403.png)
We have successfully come to the end of this project. I strongly believe you have learned a lot of things you can apply to your projects. If certain things aren’t clear you may wish to read other project articles.
The full code can be found on my GitHub page https://github.com/finxter/django_dictionary. Have a wonderful day.
Recommended: How I Built a Weather App Using Three Python Frameworks
5/5 – (1 vote)
As we all know, Django is a Python framework used for building complex web applications. Yet, it can also be used for building simple ones. In this tutorial, I’m going to show you
how I built an English dictionary app, a simple Django project for beginners.It takes time for a beginner to fully understand how Django works. But the key is consistency. If you keep building projects, you will gradually know enough to be dangerous with Django.
Scroll to the end to see the full code on GitHub!A Dictionary App?
![[Image: 1f4d6.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4d6.png)
![[Image: image-408.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-408.png)
A dictionary is an application used to get a particular word’s meaning. No matter how proficient we are in English, some words are far beyond our understanding. There are also words we may not know how to pronounce efficiently, especially those originating from a different language.
Furthermore, we may want to know a synonym or antonym of a given word. This is where a dictionary comes in handy. With a dictionary, we get to know not only the meaning of a word but how it is pronounced, what parts of speech it belongs to, and how it is used in a sentence. Students, no doubt, appreciate the importance of having a dictionary.
How Will We Get The Data?
How will we build this dictionary app so that when users search for a particular word, they get the meaning?
We have two options. Either we use
Code:
PyDictionary
Recommended: Is Web Scraping Legal?The drawback is the challenge that comes with web scraping. Many websites sometimes change their HTML structures to prevent people or bots from accessing their content. So, if an API is not actively maintained, it can quickly become useless. This is the case with
Code:
PyDictionaryDoing so will undoubtedly gain some valuable experience in web scraping, a very broad concept in Python programming. Still, we are at the mercy of website owners. They can change the HTML structures of their websites unexpectedly.
Prerequisites
![[Image: image-407.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-407.png)
This tutorial assumes you are familiar with Python, HTML, and Bootstrap. If not, please you are required to have at least a basic knowledge before attempting this project. Basic knowledge of Django, though not required, can speed up the learning process.
Getting started
Create a new folder for this project. Then you do the following:
- Create a virtual environment
Code:python3 -m venv .venv
- Activate the virtual environment
Code:source .venv/bin/activate
- Install the required libraries in the virtual environment
Code:pip install django tzdata requests bs4
- Create a file to keep a record of the module versions.Code:
requirements.txt
Code:pip freeze > requirements.txt
Creating Django Project and Application
i. Creating the project
Code:
django-admin startproject project .Code:
projectii. Creating the application
Code:
python3 manage.py startapp dictionaryCode:
manage.pyiii. Applying migrations
Code:
python3 manage.py migrateiv. Checking to see if the installation was successful
Code:
python3 mange.py runserver![[Image: image-400.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-400.png)
Once you see the above image, you are good to go.
Registering the App
![[Image: image-406.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-406.png)
- In fileCode:
settings.py
This step is important whenever an app is created. Navigate to the project folder. Open thefile and scroll down to theCode:settings.pysection.Code:INSTALLED_APPS
Code:
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # custom app 'dictionary', ]We have registered the app in the
Code:
settings.pyCode:
settings.py- In the fileCode:
urls.py
Open thefile in the project folder and add this:Code:urls.py
Code:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('dictionary.urls')), ]We have registered the app-level URL. Let’s now create it.
Creating the App URL
This will be done inside the dictionary folder. Create a
Code:
urls.pyCode:
from django.urls import path
from . import views urlpatterns = [ path('', views.homeView, name='home'), path('search', views.searchView, name='search'),
]“
Code:
from . import viewsCode:
views.pyThe empty string indicates the home page URL which is
Code:
http://127.0.0.1:8000/This is where users will be directed when the web page is opened.
For the
Code:
'search'Code:
http://127.0.0.1:8000/searchCode:
views.pyNotice the
Code:
nameCode:
path()Creating Views
![[Image: image-405.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-405.png)
In this file, we are going to perform the web scraping we mentioned earlier. The first function, the
Code:
homeView()Code:
index.htmlCode:
from django.shortcuts import render
import requests
from bs4 import BeautifulSoup as bs # Create your views here. def homeView(request): return render(request, 'index.html')But the
Code:
searchView()So, we perform web scraping on two online dictionary websites. The first will display the meaning and the second will display the synonyms and antonyms.
Code:
def searchView(request): word = request.GET['search'] response = requests.get(f'https://dictionary.com/browse/{word}') response2 = requests.get(f'https://thesaurus.com/browse/{word}') if response: soup_1 = bs(response.text, 'html.parser') meaning = soup_1.find_all('div', {'value': '1'}) meaning_1 = meaning[0].getText() else: word = f'Sorry, we couldn\'t find your word {word} in our records.' meaning = '' meaning_ = '' if response2: soup_2 = bs(response2.text, 'html.parser') synonyms = soup_2.find_all('a', {'class': 'css-1kg1yv8 eh475bn0'}) synonym_list = [] for i in range(len(synonyms)): synonym_list.append(synonyms[i].text.strip()) antonyms = soup_2.find_all('a', {'class': 'css-15bafsg eh475bn0'}) antonym_list = [] for i in range(len(antonyms)): antonym_list.append(antonyms[i].text.strip()) else: synonym_list = '' antonym_list = '' results = { 'word': word, 'meaning': meaning_1, 'synonyms': synonym_list, 'antonyms': antonym_list } return render(request, 'search.html', results)We import the
Code:
requestsCode:
get()
Recommended: My First Python Get RequestThe result is a cruel HTML code staring at you. BeautifulSoup, invaluable for web scraping does its job well to pull the data out of the HTML content. This now makes it possible to search the parsed data.
For our first search using the
Code:
find_all()Code:
BeautifulSoupSo, we use the
Code:
getText()The second and third searches look similar to the first but instead of a div, we search in a tag. The words were kept in a tag to make it easy for visitors to be redirected to that particular word if they want to find the definition.
Instead of searching for a value, we search for a CSS class. We then append the results to their respective variables. If you are new to web scraping, you may wonder how we know where those particular words reside.
The answer is found in a browser’s developer tools, preferably Chrome Developer Tools. This is the reason I suggest familiarizing yourself with HTML before attempting this project.
Finally, we render the results on the
Code:
search.htmlCode:
elseCreating Templates
![[Image: image-404.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-404.png)
Create a folder named
Code:
templatesCode:
settings.pyCode:
osCode:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], # add these 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, },
]Then create three files in the
Code:
templates- ,Code:
base.html
- , andCode:
index.html
- .Code:
search.html
The main HTML files will inherit from the
Code:
base.htmlCode:
base.htmlCode:
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dictionary App</title> <!-- CSS only --> <!-- we are getting bootstrap5 from the CDN --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head> <body> <div class="container mt-4"> <div class="row"> <div class="mt-4 p-5 bg-success text-white rounded mb-3"> <h1>Django English Dictionary</h1> </div> <div class="col-md-12"> {% block content %} <!-- here we will inject the content of every page that inherits from the base page --> {% endblock %} </div> </div> </div>
</body> </html>Then, in the
Code:
index.htmlCode:
<!-- the index page is inheriting from the base page -->
<!-- the extends tags are used for inheriting from the base page -->
{% extends 'base.html' %} <!-- the block content tags for containing content of the page -->
{% block content %} <form action="search"> <div class="input-group"> <input type="text" required class="form-control" name="search" placeholder="Search your favorite word......."> <div class="input-group-append"> <button class="btn btn-success" type="submit"> Search </button> </div> </div> </form> {% endblock %}Recall in the
Code:
searchView()Code:
'search.'There will be a problem if you use it otherwise. Finally, add the following to the
Code:
search.htmlCode:
<!-- SEARCH.HTML -->
{% extends 'base.html' %}
{% block content %} <div> <h2 align="center"> {{ word | title }} </h2> <br> <p> <b>Meaning: </b>{{meaning}} </p> <br> <p> <b>Synonyms: </b> {{ synonyms }}
</p>
<br>
<p> <b>Antonyms: </b> {{ antonyms }} </p> <p><a href="{% url 'home' %}>Home</a></p> </div>
{% endblock %}We use Django templating language to render the results of the
Code:
searchView()![[Image: image-401.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-401.png)
That is the
Code:
index.htmlCode:
search.html![[Image: image-402.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-402.png)
Do you see how each of the pages inherits everything in the
Code:
base.htmlConclusion
![[Image: image-403.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-403.png)
We have successfully come to the end of this project. I strongly believe you have learned a lot of things you can apply to your projects. If certain things aren’t clear you may wish to read other project articles.
The full code can be found on my GitHub page https://github.com/finxter/django_dictionary. Have a wonderful day.
Recommended: How I Built a Weather App Using Three Python Frameworks

