View csv file data in view

Asked

Viewed 164 times

0

I want to display data from a file in the Django template. I don’t know how. Whether in the model or in the view. Application ucsal.

Model:

from django.db import models
import pandas as pd

class Arquivo(models.Model):

    def __dados__(self):
        return pd.read_csv("tweets_classificados.csv", encoding='ISO-8859-1', sep=";", header=0)  

view  
from django.http.response import HttpResponse  
from django.shortcuts import render  
from .models import Arquivo  

def index(request):  
    Arquivo.__dados__(self) #esta linha dá erro porque não encontra self.  
    return render(request, 'ucsal/index.html')  

I don’t know if I care about pandas in the model and I pass a variable with the value of

pd.read_csv("tweets_classificados.csv", encoding='ISO-8859-1', sep=";", header=0) 

or if the way I’m doing it is the right way.

the project is in github:

https://github.com/andredsn/mineracaodados

  • In the models or in the view? If the data is static in your csv, I don’t see why save it to a model. You can read the CSV data and assemble an array in the view. After that, pass the array to the template and mount your table. Or you want to turn everything into JSON in the view and mount with Javascript on the page.

  • @Leonardo Pessoa you probably didn’t understand my doubt. I can’t read the csv file. I already created a folder inside ucsal application (ucsal/modules/file.py) and still can’t. I only find example with database. It seems to me that it is not so common to read files and display in a browser.

  • Got it. Tried to put with the PROJECT_ROOT in the path of the archive? https://stackoverflow.com/questions/13067107/read-a-local-file-in-django Otherwise, there is no error when accessing the view to open the file?

No answers

Browser other questions tagged

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