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 joke app in django.
In this article we will make the wikipedia search app using django. For searching on wikipedia we will use “wikipedia” library in python.
Creating Django Project –
First we have to install django
Ubuntu
pip install django
Then install wikipedia library
pip install wikipedia
Lets create new django project
django-admin startproject wikipedia_app
cd wikipedia_app
Then create new app in django project
python3 manage.py startapp main
Then add app name inside the settings.py inside INSTALLED_APPS
views.py
Python3
from django.shortcuts import render,HttpResponse import wikipedia # Create your views here. def home(request): if request.method = = "POST" : search = request.POST[ 'search' ] try : result = wikipedia.summary(search,sentences = 3 ) #No of sentences that you want as output except : return HttpResponse( "Wrong Input" ) return render(request, "main/index.html" ,{ "result" :result}) return render(request, "main/index.html" ) |
Create new directory templates inside that create new directory main
Inside that create new file index.html
index.html
HTML
<!DOCTYPE html> < html > < head > < title >GFG</ title > </ head > < body > < h1 >Wikipedia Search</ h1 > < form method = "post" > {% csrf_token %} < input type = "text" name = "search" > < button type = "submit" >Search</ button > </ form > {% if result %} {{result}} {% endif %} </ body > </ html > |
Create new file urls.py inside the main app
Python3
from django.urls import path from .views import * urlpatterns = [ path('', home,name = "home" ), ] |
wikipedia_app/urls.py
Python3
from django.contrib import admin from django.urls import path,include urlpatterns = [ path( 'admin/' , admin.site.urls), path('',include( "main.urls" )), ] |
To run this app open cmd or terminal
python3 manage.py runserver
Output :-
<!–
–>
Please Login to comment…