Python | Django News App

Django is a high-level framework which is written in Python which allows us to create server-side web applications. In this article, we will see how to create a News application using Django.

We will be using News Api and fetch all the headine news from the api. Read more about the api here news api.

Do the Following steps in command prompt or terminal:

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



Create a “templates” folder in your newsapp and it in settings.py

Settings .py

In views.py

In views, we create a view named index which takes a request and renders an html as a response. Firstly we import newsapi from NewsApiClient.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing api
from django.shortcuts import render
from newsapi import NewsApiClient
  
# Create your views here. 
def index(request):
      
    newsapi = NewsApiClient(api_key ='YOURAPIKEY')
    top = newsapi.get_top_headlines(sources ='techcrunch')
  
    l = top['articles']
    desc =[]
    news =[]
    img =[]
  
    for i in range(len(l)):
        f = l[i]
        news.append(f['title'])
        desc.append(f['description'])
        img.append(f['urlToImage'])
    mylist = zip(news, desc, img)
  
    return render(request, 'index.html', context ={"mylist":mylist})

chevron_right


 

Create a index.html in templates folder.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Optional theme -->
  </head>
  <body>
    <div class="jumbotron" <style="color:black">
  
      <h1 <style ="color:white">
   Get The latest news on our website
  
    </div>
  
  
    <div class="container">
      {% for new, des, i in mylist %}
              <img src="{{ i }}" alt="">
              <h1>news:</h1> {{ new }}
              {{ value|linebreaks }}
  
              <h4>description:</h4>{{ des }}
              {{ value|linebreaks }}
  
      {% endfor %}
    </div>
  
  </body>
</html>

chevron_right


 

Now map the views to urls.py

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.contrib import admin
from django.urls import path
from newsapp import views
  
urlpatterns = [
   path('', views.index, name ='index'),
    path('admin/', admin.site.urls),
]

chevron_right


Your output of the project should look like this –



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.