Last Updated : 23 Jul, 2025
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 PackagesFirst, install Django and the pyjokes package using pip:
Create the Django Projectpip install django
pip install pyjokes
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.pyIn jokeapp/settings.py, add the main app to the INSTALLED_APPS list:
Create urls.py in the Main AppInside 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
Random joke displayed each time you refresh the page!RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4