Django server can't find a file

Hello, so I’m trying to deploy a onnx file to the browser. I don’t think a onnx file falls under the category of a static file. It’s a file that contains a machine learning model. But for the javascript code used to deploy it to the browser, I need to pass the path to onnx file. So I passed the absolute path to the loadModel function but the server can’t seem to find it even though it’s the absolute path. I have screenshots of directory, code, and error below.



You can’t give absolute path. It needs to be django static path.

  1. add your static base path to settings.py.
STATIC_URL = "static/"

If you need, add

STATIC_ROOT = "static/"

also.

  1. Add static url support for main urls.py.
    (in development server only)
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  1. set the url in javascript with django proper way.
{% load static %} 

{% static "sub folder in your static folder" %} 
{% load static %}

<img src="{% static 'path to file without static folder name/example.jpg' %}" alt="My image">
  1. copy your model file into django static folder

Side note: Please do not post images of code, templates, console output, html, error messages or any other textual data. Copy/paste the text into the body of your posted, marked as preformatted text.

Couple thoughts I’d like to expand upon:

In general, files being handled by Django are handled one of three ways:

  • A static file. A static file is any file that exists at the time the server is started, and is not likely to change in any situation other than redeploying your project.
  • A media file. In this context, a media file is a file that has been uploaded by the user. The file does not need to exist (and probably doesn’t exist) when the system is originally deployed.
  • Programmatically generated. This would be a file created or read and sent in a Django view using the HttpFileResponse.

You should categorize any file that your server needs to serve into one of these three categories.

This isn’t an accurate statement.

You request data from the server via a URL. What that URL gets mapped to is the responsibility of the server.

You never are requesting an absolute path. You are always requesting a URL. It is up to you to ensure that the server knows how to map the URL to a path on the file system.