Hello, I am extremely new to Django and trying to learn on my own. I am making progress slowly, but I am stuck on one scenario and not sure if it can be accomplished or not.
I currently call 2 separate external APIs for different purposes:
1st purpose - search a site based on a search term the user provides. This is built into the form itself for the search and basically returns a list of news articles:
forms.py
def search(self):
result = {}
search_term = self.cleaned_data['topic']
api_key = settings.NEWS_API
endpoint = 'https://api.thenewsapi.com/v1/news/all?api_token={api_key}&language=en&categories=politics&search={search_term}&sort=published_at&limit=5'
url = endpoint.format(api_key=api_key, search_term=search_term)
response = requests.get(url)
if response.status_code == 200:
result = response.json()
result['success'] = True
else:
result['success'] = False
if response.status_code == 404:
result['message'] = 'No entry found for "%s"' % search_term
else:
result['message'] = 'The News API is not available at the moment'
return result
2nd purpose - call an external API to populate data in a model called ‘Bias’. This is done via a script. This data is informational data about various news sources:
get_bias.py
class Command(BaseCommand):
def handle(self, *args, **options):
url = "https://media-bias-fact-check-ratings-api2.p.rapidapi.com/fetch-data"
headers = {
"x-rapidapi-key": "xxxxxxxxxxxx",
"x-rapidapi-host": "media-bias-fact-check-ratings-api2.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
decoded_response = response.content.decode('utf-8-sig')
data = json.loads(decoded_response)
if response.status_code == 200:
for i in data:
Bias.objects.update_or_create(
mbfc_id = i['Source ID#'],
defaults = {
'source_name': i['Source'],
'mbfc_url': i['MBFC URL'],
'bias': i['Bias'],
'factual': i['Factual Reporting'],
'type': i['Media Type'],
'source_url': i['Source URL'],
'credibility': i['Credibility'],
'country': i['Country'],
}
)
self.stdout.write(self.style.SUCCESS('Successful'))
else:
self.stdout.write(self.style.ERROR('Failed'))
Here is what I am trying to accomplish:
In the first search one of the fields returned in the API is called “Source”. I want to take that field that is returned and use it to search against the model ‘Bias’.
Ultimately my goal is to show all of the fields of the first “search” API and also include several fields from the model data but only IF the source is found in the model.
I have searched everything I can find but no luck - I took a stab at this myself and it’s not working.
Here is my view and what I have tried:
views.py
def news_view(request):
topic_search = {}
bias_data = Bias.objects.all().values()
if 'topic' in request.GET:
form = NewsForm(request.GET)
if form.is_valid():
topic_search = form.search()
else:
form = NewsForm()
context = {
'topic_search': topic_search,
'bias_data': bias_data,
'form': form
}
return render(request, 'news/news_api.html', context=context)
news_api.html
{% if topic_search %}
<h2>results:</h2>
{% if topic_search.success %}
{% for result in topic_search.data %}
<h3>{{ result.topic }}</h3><br>
<div class="card text-center">
<h3>{{ result.title }}</h3><br>
<p>{{ result.description }}</p><br>
<p>{{ result.published_at }}</p><br>
<p>{{ result.source }}</p><br>
{% if result.source == bias_data.source_url %}
<p>Bias: {{ bias_data.bias }}</p>
{% endif %}
<a href="{{ result.url }}">{{ result.url }}</a>
</div>
{% endfor %}
{% else %}
<p><em>{{ search_result.message }}</em></p>
Any help would be appreciated!!