Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - passing variable from function view into html template

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()),
]
like image 394
tomczas Avatar asked Sep 19 '26 23:09

tomczas


2 Answers

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.

like image 90
Ansuman Avatar answered Sep 22 '26 11:09

Ansuman


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'})
like image 37
Richard Avatar answered Sep 22 '26 11:09

Richard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!