Skip to content

Commit e558570

Browse files
committed
add dict app tutorial with django
1 parent abd5c62 commit e558570

40 files changed

+344
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
210210
- [Detecting Fraudulent Transactions in a Streaming Application using Kafka in Python](https://www.thepythoncode.com/article/detect-fraudulent-transactions-with-apache-kafka-in-python). ([code](general/detect-fraudulent-transactions))
211211
- [Asynchronous Tasks with Celery in Python](https://www.thepythoncode.com/article/async-tasks-with-celery-redis-and-flask-in-python). ([code](https://github.com/bassemmarji/flask_sync_async))
212212
- [How to Build a CRUD App with Flask and SQLAlchemy in Python](https://www.thepythoncode.com/article/building-crud-app-with-flask-and-sqlalchemy). ([code](web-programming/bookshop-crud-app-flask))
213+
- [How to Build an English Dictionary App with Django in Python](https://www.thepythoncode.com/article/build-dictionary-app-with-django-and-pydictionary-api-python). ([code](web-programming/djangodictionary))
213214

214215
- ### [GUI Programming](https://www.thepythoncode.com/topic/gui-programming)
215216
- [How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python). ([code](gui-programming/text-editor))

web-programming/djangodictionary/dictionary/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DictionaryConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'dictionary'

web-programming/djangodictionary/dictionary/migrations/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Dictionary</title>
9+
<!-- CSS only -->
10+
<!-- we are getting bootstrap5 from the CDN -->
11+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
12+
</head>
13+
14+
<body>
15+
<div class="container mt-4">
16+
<div class="row">
17+
18+
<div class="mt-4 p-5 bg-success text-white rounded mb-3">
19+
<h1>ThePythonCode.com Dictionary</h1>
20+
</div>
21+
22+
<div class="col-md-12">
23+
{% block content %}
24+
<!-- here we will inject the content of every page that
25+
inherits from the base page -->
26+
{% endblock %}
27+
</div>
28+
</div>
29+
</div>
30+
</body>
31+
32+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- the index page is inheriting from the base page -->
2+
<!-- the extends tags are used for inheriting from the base page -->
3+
{% extends 'dictionary/base.html' %}
4+
5+
<!-- the block content tags for containing content of the page -->
6+
{% block content %}
7+
8+
<form action="search">
9+
<div class="input-group">
10+
<input type="text" required class="form-control" name="search" placeholder="Search your favorite word.......">
11+
<div class="input-group-append">
12+
<button class="btn btn-success" type="submit">
13+
Search
14+
</button>
15+
</div>
16+
</div>
17+
18+
</form>
19+
20+
{% endblock %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!-- the search page inherits from the base -->
2+
{% extends 'dictionary/base.html' %}
3+
4+
{% block content %}
5+
<!-- this will display the searched word -->
6+
<h4>{{ word }}</h4>
7+
8+
<form action="" method="post">
9+
{% csrf_token %}
10+
<input type="hidden" name="{{ word }}" value="">
11+
<button type="submit"><svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
12+
<path stroke-linecap="round" stroke-linejoin="round" d="M15.536 8.464a5 5 0 010 7.072m2.828-9.9a9 9 0 010 12.728M5.586 15H4a1 1 0 01-1-1v-4a1 1 0 011-1h1.586l4.707-4.707C10.923 3.663 12 4.109 12 5v14c0 .891-1.077 1.337-1.707.707L5.586 15z" />
13+
</svg></button>
14+
</form>
15+
<!-- this will display the word meaning -->
16+
<p>{{ meanings }}</p>
17+
18+
<hr>
19+
<!-- this will display the antonym for the word if its available-->
20+
<p><b>Antonyms</b>:{{ antonym }}</p>
21+
<hr>
22+
<!-- this will display the synonym for the word if its available-->
23+
<p><b>Synonyms</b>:{{ synonym }}</p>
24+
25+
{% endblock %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# from current folder, we are importing the two views, HomeView & SearchView
2+
from . import views
3+
# importing path from django's in-built urls
4+
from django.urls import path
5+
6+
# defining the list for urls
7+
urlpatterns = [
8+
path('', views.homeView, name='home'),#this is the home url
9+
path('search', views.searchView, name='search'),#this is the search url
10+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# importing the render function from django.shortcuts
2+
# the render function renders templates
3+
from django.shortcuts import render
4+
# importing the PyDictionary library
5+
from PyDictionary import PyDictionary
6+
7+
import pyttsx3
8+
9+
10+
# this is the view that will render the index page
11+
def homeView(request):
12+
return render(request, 'dictionary/index.html')
13+
14+
15+
# this is the view that will render search page
16+
def searchView(request):
17+
# capturing the word from the form via the name search
18+
word = request.GET.get('search')
19+
# creating a dictionary object
20+
dictionary = PyDictionary()
21+
# passing a word to the dictionary object
22+
meanings = dictionary.meaning(word)
23+
# getting a synonym and antonym
24+
synonyms = dictionary.synonym(word)
25+
antonyms = dictionary.antonym(word)
26+
# bundling all the variables in the context
27+
context = {
28+
'word': word,
29+
'meanings':meanings,
30+
'synonyms':synonyms,
31+
'antonoyms':antonyms
32+
}
33+
return render(request, 'dictionary/search.html', context)
34+
35+

web-programming/djangodictionary/djangodictionary/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for djangodictionary project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangodictionary.settings')
15+
16+
application = get_asgi_application()
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
Django settings for djangodictionary project.
3+
4+
Generated by 'django-admin startproject' using Django 4.0.4.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/4.0/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
15+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
16+
BASE_DIR = Path(__file__).resolve().parent.parent
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'django-insecure-ukc=wh953&q0x-s!b2i28^nh!)3r#^28(ougohz)yruya)))^d'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
# external installed app
41+
# registering the dictionary app
42+
'dictionary',
43+
]
44+
45+
MIDDLEWARE = [
46+
'django.middleware.security.SecurityMiddleware',
47+
'django.contrib.sessions.middleware.SessionMiddleware',
48+
'django.middleware.common.CommonMiddleware',
49+
'django.middleware.csrf.CsrfViewMiddleware',
50+
'django.contrib.auth.middleware.AuthenticationMiddleware',
51+
'django.contrib.messages.middleware.MessageMiddleware',
52+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
53+
]
54+
55+
ROOT_URLCONF = 'djangodictionary.urls'
56+
57+
TEMPLATES = [
58+
{
59+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
60+
'DIRS': [],
61+
'APP_DIRS': True,
62+
'OPTIONS': {
63+
'context_processors': [
64+
'django.template.context_processors.debug',
65+
'django.template.context_processors.request',
66+
'django.contrib.auth.context_processors.auth',
67+
'django.contrib.messages.context_processors.messages',
68+
],
69+
},
70+
},
71+
]
72+
73+
WSGI_APPLICATION = 'djangodictionary.wsgi.application'
74+
75+
76+
# Database
77+
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
78+
79+
DATABASES = {
80+
'default': {
81+
'ENGINE': 'django.db.backends.sqlite3',
82+
'NAME': BASE_DIR / 'db.sqlite3',
83+
}
84+
}
85+
86+
87+
# Password validation
88+
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
89+
90+
AUTH_PASSWORD_VALIDATORS = [
91+
{
92+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93+
},
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
102+
},
103+
]
104+
105+
106+
# Internationalization
107+
# https://docs.djangoproject.com/en/4.0/topics/i18n/
108+
109+
LANGUAGE_CODE = 'en-us'
110+
111+
TIME_ZONE = 'UTC'
112+
113+
USE_I18N = True
114+
115+
USE_TZ = True
116+
117+
118+
# Static files (CSS, JavaScript, Images)
119+
# https://docs.djangoproject.com/en/4.0/howto/static-files/
120+
121+
STATIC_URL = 'static/'
122+
123+
# Default primary key field type
124+
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
125+
126+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""djangodictionary URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/4.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
# importing the django's in-built admin url
17+
from django.contrib import admin
18+
# importing path and include from django's in-built urls
19+
from django.urls import path, include
20+
21+
# defining the list for urls
22+
urlpatterns = [
23+
path('admin/', admin.site.urls),
24+
# registering dictionary app urls in project
25+
path('', include('dictionary.urls')),
26+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for djangodictionary project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangodictionary.settings')
15+
16+
application = get_wsgi_application()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangodictionary.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

0 commit comments

Comments
 (0)