Create Joke App in Django from Scratch

Django REST Framework is a robust and flexible toolkit for building Web APIs. It is written in python and allows the user to create some cool and awesome web applications. In this blog we will learn how to create a joke app from scratch in Django from scratch. This will be another good project for you to show. Now coming back to our project we will be using pyjokes library for generating jokes. Pyjokes actually generate one line jokes, so everytime you refresh your page a new joke will pop up, isn’t it cool you can use this functionality in your other projects and can make it more user friendly .

Create Joke App in Django from Scratch

Implementations

Step 1. Create Django Project

We are first going to create a django project, an app inside that project and then going to install pyjokes, which will play a major role in our project.
1. Create a Django project.

django-admin startproject blog19

2. Create an app in that django-project.

python manage.py startapp jokes

3. Install pyjokes, pyjokes will provide you jokes.

pip install pyjokes

4. Add your app name in installed apps.

Settings.py

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

Step 2. Add files and Folder to the Django Project

We need to create a template folder in the django folder and a urls.py file in the app folder.

  1. Create a new folder in the django folder(here,blog19) save it with the name template.
  2. Add the path for this template folder in blog19 > settings.py .

Settings.py

import os
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'template')],
        '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',
            ],
        },
    },
]

3. Create a new file in the app folder(here, jokes) save it with the name urls.py .

4. Add path for this url.py file in blog19 > urls.py .

Urls.py

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

Step3. Create Joke app

In this step we will perform main implementation. You just need to create a html file then, provide its url path and link it with the function in views.py and then that views.py function will provide the joke that you can render in your html file.

  1. Create a path for the index html page in jokes > urls.py .

Urls.py

from django.urls import path,include
from . import views
urlpatterns = [
    path('',views.index,name='index')
]

2. Create a function for the index page in jokes > views.py .

Views.py

from django.shortcuts import render
import pyjokes
# Create your views here.
def index(request):
    jokes=pyjokes.get_joke()
    return render(request,'index.html',{'jokes':jokes})

3. Create html file in template>index.html .

Index.html

<html>
<head>
    <title>
        Joke APP
    </title>
</head>
<body>
    <h1 align="center"> Joke OF the Day</h1>
    <p align="center">{{jokes}}</p>
</body>
</html>

Output :-

Create Joke App in Django from Scratch
Joke 1
Create Joke App in Django from Scratch
Joke 2

Quick Revision

  1. Create your django project folder.
  2. Create an app folder in the django project folder.
  3. Install pyjokes.
  4. Add template folder in the django folder and provide its path in django_folder > settings.py .
  5. Create a file named as urls.py in the app folder and provide its path in django_project > urls.py.
  6. Add path of index html page in app_folder > urls.py.
  7. Create function for the index html page in app_folder > views.py.
  8. Add html code for the  in django_project > template > index.html.

GitHub Link :-

https://github.com/jaya2320/django_joke_app

Conclusion

So, we learned how to create a Joke App from scratch in Django. I hope this blog will be useful for you and helps you to understand each and every step. Hope all your doubts get cleared. Thank you.