Skip to content
Related Articles

Related Articles

Joke Application Project Using Django Framework
  • Last Updated : 15 Jan, 2021

Django is a high-level Python based Web Framework that allows rapid development and clean, pragmatic design.  It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. Today we will create a Joke app in Django.

Installation – 

pip3 install django
pip3 install pyjokes

Basic Setup –

Start a project by the following command

django-admim startproject jokeapp

Change the directory to jokeapp

cd jokeapp

Start the server- Start the server by typing following command in terminal –

python manage.py runserver

To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL.



Create main App – 

python manage.py startapp main

Goto main/ folder by doing : cd main and create a folder with index.html file : templates/main/index.html

Open the project folder using a text editor. The directory structure should look like this :

Now add main app  in your jokeapp in settings.py.

Edit the urls.py in jokeapp

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Create new urls.py in your main app (jokeapp/main/)

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.urls import path
from .views import *
  
urlpatterns = [
    path("",home, name="home"),
]

chevron_right


Edit views.py in main

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.shortcuts import render,HttpResponse
import pyjokes
# Create your views here.
  
def home(request):
    joke=pyjokes.get_joke()
    return render(request,"main/index.html",{"joke":joke})

chevron_right


Create new templates folder inside the main inside that create another folder main and create the new file index.html . (main/templates/main/index.html)

index.html

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output –

Now you can run the server to see your joke app

python manage.py runserver


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :