run a script with an html button

Hi, I’m trying to run a script that generates a .csv file when you click a button but I don’t know how to do it. my work directory is as follows:

my html in ‘templates’home.html’:

<form action = "script/one.py" method = "POST">

    <input type = "submit" value = "articulos">

</form>

my script.py in 'script/one.py:

import pandas as pd

cat = "adios"

print(cat) 

my view in /articulos/view.py:

from django.shortcuts import render

from django.views.generic import ListView

from .models import Articulo

class ArticuloListView(ListView):

    model = Articulo

    template_name = 'home.html'

    import os

    os.system('python script/one.py')

I use os.system to call the script and it gets into it but when I import the “pandas” library to create .csv files I get the following error:

Performing system checks…

Traceback (most recent call last):
File “C:\Users\Minuke\Desktop\prueba\script\one.py”, line 1, in
import pandas as pd
ModuleNotFoundError: No module named ‘pandas’
System check identified no issues (0 silenced).
November 29, 2020 - 13:50:22
Django version 3.1, using settings ‘config.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

and this happens with any import, I don’t know very well how to do to run a script completely using django

It appears that the basic issue is that your environment isn’t properly set up when trying to run it. But I have to wonder why you’re trying to run this as an external script rather than just putting the work you’re really trying perform in your view (or a function that the view calls).

1 Like

I forget to import the “pandas” library for my virtual environment, sorry i’m new to django