Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

419
Views
Django's static() function to retrieve static file gives no such file -error

I'm doing a project with Django with the following structure:

/project
   /cv
      /static
        /configuration
           configuration.json

So a project with one app in it and a config.json file in the static folder.

My settings.py (the most important settings for this):

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "cv",
]
STATIC_URL = "/static/"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, "cv/static")

In my view I use the static() function to retrieve the static file

def index(request):
    file_path = static("configuration/configuration.json")
    with open(file_path) as f:
        configuration = json.loads(f.read())
    return render(request, "index.html", configuration)

But It keeps giving me the error:

No such file or directory: '/static/configuration/configuration.json'

I can fix it by explicitly parsing the path string to the index function:

def index(request):
    file_path = "./cv/static/configuration/configuration.json"
    with open(file_path) as f:
        configuration = json.loads(f.read())
    return render(request, "index.html", configuration)

But how do I do it with the static() function? The static() function uses the static_url variable, but no matter how I adjust it, it keeps giving me a similar error.

Anyone an idea what I'm doing wrong here?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The static() function is going to return the URL that the file can be accessed at, you don't need that, you need to get the path to the file on the filesystem.

Join settings.STATIC_ROOT and the file to get the path to the file on the filesystem

def index(request):
    file_path = os.path.join(settings.STATIC_ROOT, "configuration/configuration.json")
    with open(file_path) as f:
        configuration = json.loads(f.read())
    return render(request, "index.html", configuration)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!