How to Build News Application in Django

How to Build News Application in Django

In this Python Django tutorial, you'll learn How to Build News Application in Python Django, so we are using Django library for this tutorial, also for fetching the news we are going to use News API. What is Django? Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Python Django Build News Web Application - Python Web Development.

In this article iam going to show you How to Build News Application in Python Django, so we are using Django library for this article, also for fetching the news we are going to use News API , so first of all what is django.

What is Django ?

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

  • **Ridiculously fast.**Django was designed to help developers take applications from concept to completion as quickly as possible.

  • **Reassuringly secure.**Django takes security seriously and helps developers avoid many common security mistakes.

  • Exceedingly scalable.

  • Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.

Installation

You can simply install django by using _pip install django _

OK after installing django you need to make account in News API, because we are going to fetch the data from their and also you need to get your api key from their. for the installation of this library you can simply use _pip install newsapi-python. _

OK now it is time to open your Pycharm IDE, after that open the terminal of Pycharm IDE and you need to use this command for creating of project in django.

django-admin startproject News

for the name of the project i have used News, but you can change it according to your choice.

After that you need to migrate your migrate your project by this command

python manage.py migrate

Now you need to create a new app in your django project, so now what are apps ? django apps are the small pieces of a website or a web application, in django project as many apps you want you can create.

python manage.py createapp NewsApp

i have created an app at name of NewsApp but you can change that name according to your choice. also you need to add your new app in the django setting Installed App section like this .

If you see in the above picture, you can see the structure of my django project, also you can see that i have created a templates folder and i have added two html files in their, so these are the html files.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Aljazera News</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">
</head>
<body>

<div class="jumbotron" style="color:black">

    <h1 style="color:black">
        This is the latest news

    </h1>

</div>


<div class="container">
    {% for new, des, i in mylist %}
    <img src="{{i}}" alt="">
    <h3>News:</h3> {{new}}
    {{value | linebreaks}}

    <h4>Description:</h4> {{des}}
    {{value | linebreaks}}



    {% endfor %}




</div>

</body>
</html>

And this is bbc.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BBC News</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">
</head>
<body>

<div class="jumbotron" style="color:black">

    <h1 style="color:black">
        This is the latest news

    </h1>

</div>


<div class="container">
    {% for new, des, i in mylist %}
    <img src="{{i}}" alt="">
    <h3>News:</h3> {{new}}
    {{value | linebreaks}}

    <h4>Description:</h4> {{des}}
    {{value | linebreaks}}



    {% endfor %}




</div>

</body>
</html>

And this is our views.py that we have created for two url functions.

from django.shortcuts import render
from newsapi import NewsApiClient



def Index(request):
    newsapi = NewsApiClient(api_key="YOUR API KEY")
    topheadlines = newsapi.get_top_headlines(sources='al-jazeera-english')


    articles = topheadlines['articles']

    desc = []
    news = []
    img = []

    for i in range(len(articles)):
        myarticles = articles[i]

        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])


    mylist = zip(news, desc, img)


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



def bbc(request):
    newsapi = NewsApiClient(api_key="YOUR API KEY")
    topheadlines = newsapi.get_top_headlines(sources='bbc-news')


    articles = topheadlines['articles']

    desc = []
    news = []
    img = []

    for i in range(len(articles)):
        myarticles = articles[i]

        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])


    mylist = zip(news, desc, img)


    return render(request, 'bbc.html', context={"mylist":mylist})

Also one important issue you need to create a new python file in your NewsApp django app and add these codes for url routing.

from django.urls import path
from .views import Index, bbc

urlpatterns = [
path('', Index, name = 'Index'),
path('bbc/', bbc, name = 'BBC')

]

Also in your main urls.py you need to add these codes.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('NewsApp.urls'))
]

So now run your django project and this will be the result, you can use this command for runing your django project.

python manage.py runserver

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Python Django Tutorial | Django Course

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

This Python Django tutorial will help you learn what is django web development & application, what is django and introduction to django framework, how to install django and start programming, how to create a django project and how to build django app. There is a short django project as well to master this python django framework.

Why should you watch this Django tutorial?

You can learn Django much faster than any other programming language and this Django tutorial helps you do just that. Our Django tutorial has been created with extensive inputs from the industry so that you can learn Django and apply it for real world scenarios.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Hire Now: https://bit.ly/394wdOx