Open In App

Joke Application Project Using Django Framework

Last Updated : 20 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We will create a simple Joke application using Django. By using the pyjokes package, we’ll build a web app that generates and displays random jokes. We’ll go step-by-step to set up the project, configure the views, and render jokes dynamically on the homepage.

Install Required Packages

First, install Django and the pyjokes package using pip:

pip install django
pip install pyjokes

Create the Django Project

Prerequisites:

Create a new Django project:

django-admim startproject jokeapp
cd jokeapp
python manage.py startapp main

The directory structure should look like this :

Update settings.py

In jokeapp/settings.py, add the main app to the INSTALLED_APPS list:

Create urls.py in the Main App

Inside the main folder, create a new file urls.py:

main/urls.py:

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

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

Update urls.py in the Project

Edit the jokeapp/urls.py to include the main app's URLs:

jokeapp/urls.py:

Python
from django.urls import path
from .views import *

urlpatterns = [
    path("", home, name="home"),
]

Create views.py for Joke Logic

Edit the views.py file in the main app to get a joke from the pyjokes package:

main/views.py:

Python
from django.shortcuts import render,HttpResponse
import pyjokes

def home(request):
    joke=pyjokes.get_joke()
    return render(request,"main/index.html",{"joke":joke})

Create Templates for Rendering the Joke

Create the folder structure for templates inside the main app:

main/templates/main/index.html

Inside the main/templates directory, create another directory called main, and inside that folder, create the index.html file.

HTML
<html>
  <head>
    <title>Home Page</title>
  </head>
<body>
<h3>{{joke}}</h3>
</body>
</html>

Run the Server

Once everything is set up, start the server again:

python manage.py runserver

Output

jokeApplicationUsingDjango
Random joke displayed each time you refresh the page!

Next Article

Similar Reads