0

I'm trying to have a table that opens an invoice in a new tab but I'm not getting on how to use the url format to have the mapping done to a shared network folder.

enter image description here

I'm trying by doing the following in the table definition

  <tbody>
  {% for datarow in dataset %}
  <tr>
    <td>{{datarow.supplierName}}</td>
    <td>{{datarow.invoiceNumber}}</td>
    <td>{{datarow.reference}}</td>
    <td><a href = "open(r'\\REMOTESERVER\Shared1\FOLDER\FOLDER Images\{{datarow.docId|urlize}}\file.tiff')">Open Invoice</a>
    {% endfor %}
  </tr>
  </tbody>

however it doesn't open as the url still tries to map from localhost.

Also in each folder I have one .tiff file but other files as well, I'm also looking into having a way to open that .tiff file (different name per folder) to be used in the url but I haven't got there yet.

note: my datarow.docId is the folder name in the network drive. Also I've read that it might have to be done with Pathlib, however I don't get how to do it if so.

Baldie47
  • 1,148
  • 5
  • 16
  • 45

3 Answers3

1

I was able to solve this by setting the shared drive in the "STATICFILES_DIRS" list and then

{% load static %}
    {% static "" %}{{ datarow.docud }}/file.tiff

to call in the template for the static path

Baldie47
  • 1,148
  • 5
  • 16
  • 45
0

I don't know what is your purpose to use hyperlink address in that way. But IMO, I assume you have a folder in some server that you put your files there. So I think there is a way to make a DNS address to the main link to server and main folder you are using and add it as MEDIA_URL in your settings file.

I hope this relate to your real need and can help,

  • yes, my files (the invoices) are in a shared network drive (not the same server where the django site would be, but accessible from it) I'll check this media url, I'm very new to django so I don't quite understand. of course if you have a better suggestion rather than opening a remote file as I'm looking to do, I'm open to it :) thank you! – Baldie47 Jun 15 '21 at 11:36
  • @Baldie47 If you just create that invoice and want to show its URL in the list you may add a FileField to the model you are using to fill that table and get the URL from file field. As you did not mention anything about the model behind that html template I can not say anything more. – Ali javanmardi Jun 15 '21 at 11:48
  • no, haven't created it, this site has the purpose of showing a historic of several invoices. so it's a webserver that has the site, and has to fetch the invoices from a shared drive. I'm reading all the column information from an SQL server db and the file needs to come from a shared drive location – Baldie47 Jun 15 '21 at 11:50
0

I am not sure how the browser interprets the open command in your href. Have you tried a file: URI?

<a href="file://REMOTESERVER/shared1/folder/folder%20images/{{datarow.docId|urlize}}/file.tiff" target="_blank">Open Invoice</a>

The browser needs to have access to the remote server for this to work. This might a situation in an intranet environment. On the open internet uploadable MEDIA_URL might be the right solution.

The target="_blank" attribute opens a new tab or window for the document.

Additionally, you might want to check if </tr> belongs inside our outside your for loop.

Fabian
  • 571
  • 5
  • 24
  • I corrected based on your additional. Also, using the "file://" gives me error when trying in the browser "Not allowed to load local resource: " which I think is a block from chrome in this case (since it works in the old IE but not the best solution) I should find a way to fetch the file from the shared drive and then serve it to the user better, right? – Baldie47 Jun 15 '21 at 11:44
  • If you want Django to `pipe` the file through, you will need to create a view to serve the file. Maybe see here: https://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files – Fabian Jun 16 '21 at 13:09