Home > OS >  How can I put data from api to django
How can I put data from api to django

Time:11-18

I have stored some data in python and now I want to display it in django how can I do that?



animeUrl = "https://api.jikan.moe/v4/top/anime"
animeResponse = requests.get(animeUrl).json()

def topAnime():
    for idx, video in enumerate(animeResponse['data']): # z [data] wyciaga mi nastepujace rzeczy ktorze sa pod spodem
        animeUrl = video['url']
        title = video['title']
        status = video['status']
        type =  video['type']   
        images = video['images']['jpg']['image_url']   
        #if status == "Currently Airing":
        print (idx 1,":",animeUrl, ":", status, ":", title, ":", type, ":", images)    
topAnime()


this is my stored data and now I want to display it on website, how can I do that? I'm newbie in django I'm looking for some suggestions

I have tried using templates but it didn't worked

CodePudding user response:

The question is answered here : How to pass data to a template in Django?

In order to display data in django from url to html files there are two ways

Method 1: Rendering the template along with the data Django Templates

How to use it in the project : Render Html Pages in django

You can easily set up the jinja syntax with the help of above two links

Method-2 : Using django Rest Framework Django Rest Framwork

Prefer this method if you have already worked with api's and worked with sending ajax requests with java scripts

Sample Code structure for Method - 1: main.py

from django.shortcuts import render
animeUrl = "https://api.jikan.moe/v4/top/anime"
animeResponse = requests.get(animeUrl).json()

def topAnime():
    for idx, video in enumerate(animeResponse['data']): # z [data] wyciaga mi nastepujace rzeczy ktorze sa pod spodem
        animeUrl = video['url']
        title = video['title']
        status = video['status']
        type =  video['type']   
        images = video['images']['jpg']['image_url']   
        #if status == "Currently Airing":
        print (idx 1,":",animeUrl, ":", status, ":", title, ":", type, ":", images)    
topAnime()

def requestSender(request):
    return render(response, "index.html", data=topAnime())

index.html

<body>
<p> {{ data }} </p>
</body>

CodePudding user response:

If you are new, I highly recommend that you build your first project by following the tutorial, it helps you grasp some key concepts. You can find your solution at the third part.

But, to answer your question:

In your App's views.py:

from django.shortcuts import render
import requests
    
def get_animes(request):
    url= "https://api.jikan.moe/v4/top/anime"
    response= requests.get(url).json()
    return render(request, 'animes.html', { 'data': response['data']})

In your App's urls.py:

from django.urls import path
from . import views
    
urlpatterns = [
    path('animes/', views.get_animes, name='animes'),
]

In your animes.html file:

{% for obj in data %}
    {{ obj.url }}
    <br>
    {{ obj.title }}
    <br>
    {{ obj.status }}
    <br>
    {{ obj.type }}
    <br>
    {{ obj.jpg.image_url }}
    <br>
    <br>
{% endfor%}

In your root urls.py:

from django.urls import path, include

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

Finally, run your development server:

python manage.py runserver

Open your browser and send a request to your URL:

http://localhost:8000/animes/
  • Related