3

Good afternoon, I have a problem and I can't figure it out how to do it. I am using a third party api for data, I am storing it into metar variable and passing it as argument. However it is not working. Any help will be appreciated. Thanks.

Views.py

from urllib.request import Request, urlopen
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView
from django.template.response import TemplateResponse
# Create your views here.
class DashboardView(TemplateView):
    template_name = "index.html"

def index(request, template_name="index.html"):
    headers = {
  'Authorization': 'my_private_api'
    }
    args={}
    request = Request('https://avwx.rest/api/metar/KJFK', headers=headers)
    response_body = urlopen(request).read()
    args['metar'] = response_body
    return TemplateResponse(request,template_name,args)

index.html

{%block content %}
<div>
   <h1>Metary</h1>
   <p>{{ metar }}</p>
</div>
{%endblock content%}

urls.py

from django.urls import path
from . import views
from dashboard.views import DashboardView

urlpatterns = [
    path('', DashboardView.as_view()),
]

3 Answers 3

2

i am going to answer your question in terms of django, you will have to figure out how to get request from external api from their docs.

According to django docs: A TemplateView's Method Flowchart is

  1. setup()
  2. dispatch()
  3. http_method_not_allowed()
  4. get_context_data()

now you are using

def index(request, template_name="index.html"):
    headers = {'Authorization': 'my_private_api'}
    args={}
    request = Request('https://avwx.rest/api/metar/KJFK', headers=headers)
    response_body = urlopen(request).read()
    args['metar'] = response_body
    return TemplateResponse(request,template_name,args)

which is not going to work beacuse this def index(... is not executed at all. so you have nothing in your context metar

so changing your code to:

class DashboardView(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        #headers = {'Authorization': 'my_private_api'}
        #request = Request('https://avwx.rest/api/metar/KJFK',headers=headers)
        #response_body = urlopen(request).read()

        context['metar'] = 'some information'
        return context

will give you metar as 'some information' in your template.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use the render function to simplify this.

e.g.

from django.shortcuts import render

def index(request, template_name="index.html"):
    return render(request, template_name, {'metar': 'Hello world'})

2 Comments

i made it but still when i pass {{metar}} in index.html i get nothing.
Your 'def index' view isn't in your urlpatterns? So it's using DashboardView instead. Try and add it there instead of DashboardView.
0

After looking at your code, I got to know that you have two views as DashboardView which is a template view and another index which is function based view (and you have configured it improperly). In Urls you have configured DashboardView as your view handler and your actual api code lies in index that's why it was not working. Following solution is based on function based view, this might help.

views.py

from django.shortcuts import render
from urllib.request import Request, urlopen

def index(request):
    headers = {'Authorization': 'my_private_api'}
    request = Request('https://avwx.rest/api/metar/KJFK', headers=headers)
    response_body = urlopen(request).read()
    context = {
    'metar':response_body
    }
    return render(request,'index.html',context=context)

template.html

{%block content %}
<div>
   <h1>Metary</h1>
   <p>{{ metar }}</p>
</div>
{%endblock content%}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index ),
]

1 Comment

I have not tested it as it contain api calls, but if you get any errors let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.