B
B
butroskali2020-02-28 11:01:57
Django
butroskali, 2020-02-28 11:01:57

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

The summoner function is called from views so that the result of its execution is added to the html template, but this function returns an empty list. If you call the summoner function from a file, it returns correctly and returns the complete list. in connection with this, I think that django does not have access to files, now the path to them is written in the following way, tell me how correct this is?
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)


views.py
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

2 answer(s)
M
Maxim, 2020-02-28
@butroskali

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)

to absolute
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)

D
Dr. Bacon, 2020-02-28
@bacon

See how BASE_DIR is calculated in settings.py and how it is then used, cast all your paths to absolute, in a similar way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question