Answer the question
In order to leave comments, you need to log in
Why doesn't Django views 'see' files in the project directory?
There is such a structure of the django project:
- project
--- static - folder_csv
--- function.py
--- views.py
In function.py, the script of three functions 1st makes insert into db as an argument, taking the csv file that lies in the folder with the project, the second one does the same, but for another table, the 3rd function runs in a loop (the number of cycles of which depends on the number of csv files in the folder) calls the first two functions and makes a request to the database adding the results of its execution to the list, after what the list returns.
function.py code:
import pandas as pd
import psycopg2
import os, glob
conn = psycopg2.connect(host='ip_address, database='unicorn',
user='user', password='password')
cur = conn.cursor()
def insert_data_as_is(file_name):
cur.execute('truncate table test_inv.start_tbl')
with open(file_name, 'r') as file:
cur.execute("insert into test_inv.start_tbl values {0}".format(file.read()))
conn.commit()
def insert_data_to_be(file_name):
cur.execute('truncate table test_inv.res_calc_ratios_t')
with open(file_name, 'r') as file:
cur.execute('insert into test_inv.res_calc_ratios_t (test_no, test_name, hcode_id, '
'hcode_name, hcode_'
'unit_name,'
' org_id, dor_kod, duch_id, nod_id, date_type_id, metric_type_id, cargo_type_id, val_type_id,'
' unit_id, dt, value, ss, dir_id, kato_id, vids_id) values {0}'.format(file.read()))
conn.commit()
p_s = './static/csv_s/as_is/'
path_start = os.path.dirname(p_s)
start_list = []
p_f = './static/csv_s/to_be/'
path_finish = os.path.dirname(p_f)
finish_list = []
for infile in glob.glob(os.path.join(path_start, '*.*')):
start_list.append(infile)
for infile in glob.glob(os.path.join(path_finish, '*.*')):
finish_list.append(infile)
def summoner():
fun_sql_set = []
fun_query = """select * from test_inv.test_ratios('1','15')"""
for i in range(len(finish_list)):
print(finish_list[i])
insert_data_as_is(start_list[i])
insert_data_to_be(finish_list[i])
results = pd.read_sql_query(fun_query, conn)
fun_sql_set.append(results)
return fun_sql_set
p_s = './static/csv_s/as_is/'
path_start = os.path.dirname(p_s)
start_list = []
p_f = './static/csv_s/to_be/'
path_finish = os.path.dirname(p_f)
finish_list = []
for infile in glob.glob(os.path.join(path_start, '*.*')):
start_list.append(infile)
for infile in glob.glob(os.path.join(path_finish, '*.*')):
finish_list.append(infile)
from django.shortcuts import render
from . import function
def index(request):
return render(request,'calculus/index.html')
def output(request):
data = function.summoner()
print(data)
return render(request,'calculus/index.html',{'data':data})
Answer the question
In order to leave comments, you need to log in
Try replacing the relative path to the folder
p_s = './static/csv_s/as_is/'
path_start = os.path.dirname(p_s)
p_f = './static/csv_s/to_be/'
path_finish = os.path.dirname(p_f)
cur_dir = os.path.dirname(os.path.abspath(__file__))
path_start = '{}/static/csv_s/as_is/'.format(cur_dir)
path_finish = '{}/static/csv_s/to_be/'.format(cur_dir)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question