Reputation: 1254
I need to make a dynamic path for a file that I want to open. I need to retrieve the .tif file from within a path.
this path is formed by a combination of a "STATICFILES_DIRS" from my settings.py and a field ccalled docId from my models.py that get filled from the views.py
now, I'm very new to django so I really don't understand what I dont know (if this makes sense)
my views.py has the following:
from django.shortcuts import render
import pyodbc
# Create your views here.
def home(request): conn=pyodbc.connect('Driver={sql server};'
'Server=server.domain.com;' 'Database=database;' 'Trusted_Connection=yes;')
cursor = conn.cursor() cursor.execute("select top 100 docId, supplierName,
invoiceNumber, reference from table") result = cursor.fetchall() return
render(request, 'azolve/home.html', {'Azolve': result})
and my models.py has this:
from django.db import models
import os import fnmatch
class Azolve(models.Model):
docId = models.IntegerField
supplierName = models.CharField(max_length=100)
invoiceNumber = models.IntegerField
reference = models.CharField(max_length=100)
# for file_name in os.listdir(str(filePath)):
# if fnmatch.fnmatch(file_name, '*.tif'):
# docIdFileName = file_name
and in my settings.py I have this part:
STATIC_URL = '/static/'
STATICFILES_DIRS = [ "//server1/Shared1/folder/ Migration Images/", ]
so, to give an idea, each docId that gets retrieved is the name of a folder contained in the path "STATICFILES_DIRS " what I was trying to do is to navigate for each folder and get the .tif file inside the folder, so I can concatenate and make the complete filename with the format:
//server1/Shared1/folder/ Migration Images/--docIDFOLDER--/FILENAME.TIF
this would later be shown in a table in my html file doing something like:
<tbody>
{% for datarow in Azolve %}
<tr>
<td>{{datarow.supplierName}}</td>
<td>{{datarow.invoiceNumber}}</td>
<td>{{datarow.reference}}</td> <td><a href= ' {% static "" %}{{ datarow.docId }}/{{ datarow.docIdFileName }}'>Open Invoice</a></td> </tr>
{% endfor %}
</tbody>
Does this makes sense? I've been checking different django tutorials, but I couldn't find an example of something similar to what I need to do :(
I don't know how should I get this data so I can concatenate and then pass it to the "datarow"
I've tried adding this in the models but I think is not correct to add it on the class, should be then in the views inside the function? as a new function?
Note: I'm not having the user upload files, these are stored in a network drive and I only need to have users look for them and download them, my problem is generating the complete path from the folder where the .tif file is stored (I get to "//Server/Folder/Folder2/docIdFolder/XXXX.tif" but I can't assign the value of the XXXX.tif part.
Upvotes: 0
Views: 63
Reputation: 1646
I would suggest adding a field to the model to store the file name, and look it up once from the file system, storing it in the db after that.
class Azolve(models.Model):
docId = models.IntegerField
supplierName = models.CharField(max_length=100)
invoiceNumber = models.IntegerField
reference = models.CharField(max_length=100)
img_filename = models.CharField(max_length=255, blank=True, null=True)
but also add a def to the Azolve model class which attempts to populate that img_filename if it doesn't exist. Something along the lines of the following which tries to locate the .tif using pathlib's glob (https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob):
# put this at the top of your models.py
from pathlib import Path
# and this right in your class
def tif_file(self):
if self.img_filename:
return self.img_filename:
else:
search_path = Path(r'//server1/Shared1/folder/Migration Images')
# the filter likely not needed, was using this in where I copied from
files_in_path = list(filter(Path.is_file, search_path.glob('*.tif')))
if files_in_path:
# may have to wrap the files_in_path with str()
# untested, writing in the s/o answer box
self.img_filename = files_in_path[0]
return files_in_path[0]
else:
return None
Then in your template you should be able to use {{datarow.tif_file}}
. From a performance standpoint, I'm not certain if this would get run everytime you reference this record or not or if it would only fire when called upon. Might be worth tracing through the debugger.
Upvotes: 2