Django News App

We can build server-side web apps using the high-level Python framework called Django. In this tutorial, we’ll look at how to use Django to build a News application. We’ll be using the News Api to retrieve all of the headline news. Visit news api to learn more about the api.

The powerful and adaptable Django REST Framework is a framework for creating Web APIs. The user can make some really cool and amazing web applications using this Python script. We’ll discover how to build a news app from scratch in Django in this blog post. You should show off this project as well. Returning to our project, we will use the news API to retrieve news.

The term “application programming interface,” sometimes known as “API,” is frequently used today. API entails providing an interface that enables data sharing or communication between several programmes. You utilise an API when you use a social media platform like Facebook to exchange instant messages or when you use a mobile phone weather app to check the weather.

Create a “templates” folder in the settings of your newsapp. Python Settings

In views.py, we construct a view called index that receives a request and responds with html. First, newsapi is imported from NewsApiClient.

Code:

# importing the required api  

from django.shortcuts import render  

from newsapi import NewsApiClient  

# Creating our views here.  

def ind (req):  

    newsapi = NewsApiClient(api_key ='YOURAPIKEY')  

    top = newsapi.get_top_headlines(sources ='techcrunch')  

    l = top['articles']  

    descri =[]  

    news =[]  

    imgi =[]  

    for i in range(len(l)):  

        fj = l[i]  

        news.append(fj['title'])  

        desc.append(fj['descript'])  

        img.append(fj['urlToImage'])  

    mylistin = zip(news, descr, imgr)  

    return render(req, 'index.html', context ={"mylist":mylistin})

In the templates folder, create an index.html.


  1. <!DOCTYPE html>  
  2. <html lang="en" dir="ltr">  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title></title>  
  6. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOysewffegv3Xipma34MD+dH/1frf84/j6cY/iJTQerg4e5y56yu59JvoRxT2MZw1T" crossorigin="anonymous">  
  7. <!-- Optional theme -->  
  8. </head>  
  9. <body>  
  10.     <divde class="megatron" style="color:black">  
  11.     <h1 style ="color:pearl white">  
  12. Find the most recent news on our website.  
  13.     </h1>  
  14.     </divde>  
  15.     <divde class="containerin">  
  16.     {% for new, des, i in mylistin %}  
  17.             <imge src="{{ i }}" alt="">  
  18.             <h1>news:</h1> {{ new }}  
  19.             {{ value|linebreaks }}  
  20.             <h4>description:</h4>{{ des }}  
  21.             {{ value|linebreaks }}  
  22.     {% endfor %}  
  23.     </divde>  
  24. </body>  
  25. </html> 

Map the views to urls.py now.

from django.contrib import admin  

#importing urls  

from django.urls import path  

from newsapp import views  

urlpatterns = [  

path('', views.indexin, name ='indexin'),  

    path('admin/', admin.site.urls),  

]

Output:

Django News App

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *