How to Build News Application in Django

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 .

Django News Web App

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

How to Build News Application in Python Django

#python #django #web-development

What is GEEK

Buddha Community

How to Build News Application in Django
Ahebwe  Oscar

Ahebwe Oscar

1620177818

Django admin full Customization step by step

Welcome to my blog , hey everyone in this article you learn how to customize the Django app and view in the article you will know how to register  and unregister  models from the admin view how to add filtering how to add a custom input field, and a button that triggers an action on all objects and even how to change the look of your app and page using the Django suit package let’s get started.

Database

Custom Titles of Django Admin

Exclude in Django Admin

Fields in Django Admin

#django #create super user django #customize django admin dashboard #django admin #django admin custom field display #django admin customization #django admin full customization #django admin interface #django admin register all models #django customization

7 Mistakes You Should Avoid While Building a Django Application

Django…We all know the popularity of this Python framework. Django has become the first choice of developers to build their web applications. It is a free and open-source Python framework. Django can easily solve a lot of common development challenges. It allows you to build flexible and well-structured web applications.

A lot of common features of Django such as a built-in admin panel, ORM (object-relational mapping tool), Routing, templating have made the task easier for developers. They do not require spending so much time on implementing these things from scratch.

One of the most killer features of Django is the built-in Admin panel. With this feature, you can configure a lot of things such as an access control list, row-level permissions, and actions, filters, orders, widgets, forms, extra URL helpers, etc.

Django ORM works with all major databases out of the box. It supports all the major SQL queries which you can use in your application. Templating engine of Django is also very, very flexible and powerful at the same time. Even a lot of features are available in Django, developers still make a lot of mistakes while building an application. In this blog, we will discuss some common mistakes which you should avoid while building a Django application.

#gblog #python #python django #building a django application #django #applications

Ahebwe  Oscar

Ahebwe Oscar

1620185280

How model queries work in Django

How model queries work in Django

Welcome to my blog, hey everyone in this article we are going to be working with queries in Django so for any web app that you build your going to want to write a query so you can retrieve information from your database so in this article I’ll be showing you all the different ways that you can write queries and it should cover about 90% of the cases that you’ll have when you’re writing your code the other 10% depend on your specific use case you may have to get more complicated but for the most part what I cover in this article should be able to help you so let’s start with the model that I have I’ve already created it.

**Read More : **How to make Chatbot in Python.

Read More : Django Admin Full Customization step by step

let’s just get into this diagram that I made so in here:

django queries aboutDescribe each parameter in Django querset

we’re making a simple query for the myModel table so we want to pull out all the information in the database so we have this variable which is gonna hold a return value and we have our myModel models so this is simply the myModel model name so whatever you named your model just make sure you specify that and we’re gonna access the objects attribute once we get that object’s attribute we can simply use the all method and this will return all the information in the database so we’re gonna start with all and then we will go into getting single items filtering that data and go to our command prompt.

Here and we’ll actually start making our queries from here to do this let’s just go ahead and run** Python manage.py shell** and I am in my project file so make sure you’re in there when you start and what this does is it gives us an interactive shell to actually start working with our data so this is a lot like the Python shell but because we did manage.py it allows us to do things a Django way and actually query our database now open up the command prompt and let’s go ahead and start making our first queries.

#django #django model queries #django orm #django queries #django query #model django query #model query #query with django

Kacey  Hudson

Kacey Hudson

1615513653

What is Django Used For? The Six Most Common Applications | Hacker Noon

Django is a web app framework based upon Python, but is not a part of Python. Python being open-source language, easy to learn and adapt, has become the programming language of choice for beginners as well as seasoned developers in recent years. Django was created to support web application development, web APIs, web services. Thanks to rising popularity of Python, Django too became a framework of choice for python developers.

But, why Django specifically? Why are popular social networking sites, online magazines and e-commerce sites using Django? Why is it so popularly used by developers? Let’s find out what Django is used for.

6 Ways Developers are Using Django

  • For scalable web applications
  • Django is used for applications with multiple user roles
  • For building speedy SaaS applications
  • Secure E-commerce and Enterprise applications
  • Django is Used for a building low cost MVP
  • For building Cross-platform applications

#django #django-in-mvp #django-in-saas #resize-image-in-django #django-tips

Ahebwe  Oscar

Ahebwe Oscar

1624172400

Django 3.2 - News on Compressed Fixtures and Fixtures Compression

In the Django 3.2 version just released I contributed with new features related to compressed fixtures and fixtures compression. In this article I have explored the topic and produced some sample benchmarks.

#django #django 3.2 #compressed fixtures #fixtures compression #django 3.2 - news on compressed fixtures and fixtures compression #news