How do I display variables on a Django page?

Asked

Viewed 219 times

1

I am new to Django and am taking some information from a web page using lxml. I would like to know how to display the values on my website.

import requests
from lxml import html
from django.shortcuts import render


def list_data(request):  
    return render(request, 'data.html', {})


def get_idhm(url):
    page = requests.get(url)
    tree = html.fromstring(page.content)
    idhm = tree.xpath('//*[@id="responseMunicipios"]/ul/li[6]/div/p[2]/text()')
    return idhm[0]


def get_population(url):
    page = requests.get(url)
    tree = html.fromstring(page.content)
    values = tree.xpath('//*[@id="responseMunicipios"]/ul/li[3]/div/p[2]/text()')
    return values[0]

that’s the view.

1 answer

0


As you can see in the example that showed, there is a method called list_data. In it you render data.html and an empty dictionary. In this dictionary you can put the values that will be displayed in HTML. Following example:

py views.

def list_data(request):  
    return render(request, 'data.html', {"message": "Hello World"})

data.html

<p>{{message}}</p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.