Python module usage in Django

Asked

Viewed 247 times

1

I’m starting with Django and Python now and I have a question that may sound silly: I can use python modules and functions - and others installed - usually in a Django application?

  • Not only can you, but you must.

2 answers

1


Since Django is a python framework, the modules developed for Python are perfectly usable within Django.

An illustrative example would be:

py views.

from django.shortcuts import render #Importação de módulo Django
import math #Importação de um módulo Python

def calcula(request):
    numero = 4
    elevado = math.pow(numero,2) #Uso do módulo
    raiz = math.sqrt(numero) # Uso do Módulo 
    context = {'elevado': elevado, 'raiz':raiz}
    return render(request, 'index.html', context)

index.html

<!DOCTYPE html>
<html>
<head>
    <title> Cálculos usando o módulo MATH</title>
</head>
<body>
    <h1>Exemplo simples do uso de Módulos do Python</h1>
    <p>O numero 4 elevado a 2 é {{elevado}}</p>
    <p>A raiz de 4 é  {{raiz}}</p>
</body>
</html>

inserir a descrição da imagem aqui

You are free to use the Python modules, but be careful not to look for any python functionality that is already implemented in Django more efficiently. For that look at us django modules.

0

Browser other questions tagged

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