Issue with Fetching data from Api

import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(BASE_DIR, '..'))  # Adjust '..' if needed to point to your Django project's root directory

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'newsa.settings')  # Replace 'your_project_name' with your actual project name

django.setup()
import requests

from Newss import models


def get_news():
    apikey = "f894d7ab6caa85386f38bf6b0bb39f1a"
    url = "https://gnews.io/api/v4/search?q=example&lang=en&country=us&max=10&apikey={apikey}"
    data=requests.get(url)
    print("Status Code:" ,data.status_code)
    print("Response Content:", data.content) # Add this line to see the HTTP status code
    if data.status_code==200:
       print(data.json())
       return data.json()
    else:
       print('failed')
     
def save_data():
    data=get_news()
    if data and "articles" in data:
       articles=data["articles"]

       for a in articles:
          item=models.News(
             title=a['title'],
             description=a['description'],
             content=a['content'],
             url=a['url'],
             image=a['image'],
             publishedAt=a['publishedAt'],
          )
          item.save()

I am trying to fetch the data from the Gnews Api and saving it to database but the data is not saving to the database. I tried to add print statements to print print the data but nothing is printing on the terminal.The Api key is also working fine.

Side note: When posting code, templates, or error messages here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python. (I’ve taken the liberty of editing this post for you.)

1 Like

Where and how are you running this code? I’m not seeing where this would actually be executed. Is the intent to run this from a view or directly from the command line?

its the script under my apps directory and I am trying to run it on Vscode terminal

First, I would strongly encourage you to read How to create custom django-admin commands | Django documentation | Django and convert this to a true manage command.

But beyond that, I see where you’re defining these functions, but you have no code shown here that will execute them.

Where are you calling the method save_data?
I think if you call the method in the file, then it will run

Please do not post images of code here. Copy / paste the code to be referenced into the body of your post surrounded by the lines of three backtick - ` characters as mentioned above.

1 Like
    data=get_news()
    if data and "articles" in data:
       articles=data["articles"]

       for a in articles:
          item=models.News(
             title=a['title'],
             description=a['description'],
             content=a['content'],
             url=a['url'],
             image=a['image'],
             publishedAt=a['publishedAt'],
          )
          item.save() '''

just below the get_news function

But you don’t have anything running that function.

yes you are right it worked .thank you so much for your time and effort.