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

420
Views
La función estática () de Django para recuperar un archivo estático no da tal error de archivo

Estoy haciendo un proyecto con Django con la siguiente estructura:

 /project /cv /static /configuration configuration.json

Entonces, un proyecto con una aplicación y un archivo config.json en la carpeta estática.

Mi settings.py (la configuración más importante para esto):

 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")

En mi opinión, uso la función estática () para recuperar el archivo estático

 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)

Pero me sigue dando el error:

No existe tal archivo o directorio: '/static/configuration/configuration.json'

Puedo arreglarlo analizando explícitamente la cadena de ruta a la función de índice:

 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)

Pero, ¿cómo lo hago con la función estática ()? La función static() usa la variable static_url, pero no importa cómo la ajuste, me sigue dando un error similar.

¿Alguien tiene idea de lo que estoy haciendo mal aquí?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La función static() devolverá la URL a la que se puede acceder al archivo, no necesita eso, necesita obtener la ruta al archivo en el sistema de archivos.

Únase a settings.STATIC_ROOT y el archivo para obtener la ruta al archivo en el sistema de archivos

 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!