Using an external API in Django

I'm trying to use an external API to grab data for my project to show on the template.

service.py

def get_data(title, url, description, body, datePublished):
  url = 'https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/WebSearchAPI'
    params = {"autoCorrect": "true", "pageNumber": "1", "pageSize": "10", "q": "police", "safeSearch": "true" }
    r = requests.get(url, params=params)
    data = r.json()
    article_data = {'data': data['value']}
    return article_data

Then I show it on views.py

...
import service

class IndexData(TemplateView):
    def get(self, request):
        article_data = service.get_data.all()
        return render(request, 'pages/home.html', article_data)

but I'm getting ModuleNotFoundError: No module named 'service'

Did i miss something?

Upvotes: 1

Views: 4206

Answers (2)

Answered my own problem with the help from Saint Peter on freeCodeCamp's discord channel.

Apparently, in cookiecutter-django, you have to pass from the project to the app to the view before you can import something like this:

from project_name.app_name import services

Cheers

Upvotes: 1

Nicola Montrone
Nicola Montrone

Reputation: 1

Is the main folder a python package? Does it contains the "init.py" file? If not, try to add it and retry - if it give you errors please share the file hirecracy view (even scrubbed) to have more informations

PS: if you add the init file, make sure to edit all other files that calls on that name reference e.g. "main_folder.sub_file" become just "sub_file"

Upvotes: 0

Related Questions