Using Python module in Django

0

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

    
asked by anonymous 05.10.2017 / 21:34

2 answers

1

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

An illustrative example would be:

views.py

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>

YouarefreetousethePythonmodules,butbecarefulnottolookforanypythonfunctionalitythatisalreadyimplementedindjangomoreefficiently.Foralookatthe Django modules .

    
05.10.2017 / 23:12
0

Yes, of course! Even all the libs available on link can be installed and used in the project.

    
19.10.2017 / 16:08