in Education by
My Django app is unable to see MEDIA directory. When I run one of my views I need to open a file. The scenario is to: take a record with requested id from data base get the file path to a file send the file to external server for OCR purposes urls.py from django.conf.urls.static import static urlpatterns = [ #my urls ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) models.py from django.db import models from django.contrib.auth.models import User class Homework( models.Model): title = models.CharField(max_length = 200, default = None) image = models.FileField(upload_to='user_scans/') author = models.ForeignKey(User, default=None, on_delete = models.CASCADE) latex = models.TextField(default = "No LaTeX Here") settings.py: from pathlib import Path from django.conf.urls.static import static BASE_DIR = Path(__file__).resolve().parent.parent ... DEBUG = True ... STATIC_ROOT = BASE_DIR / 'static' STATIC_URL = '/static/' MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = "/media/" views.py import requests import json from django.shortcuts import render, get_object_or_404 from .models import Homework def create_homework(request): if request.method == 'GET': #some GET stuff if request.method == 'POST': homework = Homework() homework.title = title homework.image = image homework.author = author homework.save() id = homework.id json_to_mathsnip(id) .... def json_to_mathsnip(id): homework = get_object_or_404(Homework, pk=id) f = open(homework.image.url, "rb") ... some later stuff ... Unfortunately I'm constantly running into an error: FileNotFoundError at /homework/new [Errno 2] No such file or directory: '/media/user_scans/kwa.png' My main concern is I can access file from localhost:8000/media/user_scans/kwa.png and from admin panel. Requested file is saved properly: Also settings.py configuration seems to be in tact. What might be the issue? (env) $ pip freeze asgiref==3.5.0 backports.zoneinfo==0.2.1 certifi==2021.10.8 charset-normalizer==2.0.12 Django==4.0.1 idna==3.3 Pillow==9.0.0 psycopg2-binary==2.9.3 requests==2.27.1 sqlparse==0.4.2 urllib3==1.26.9 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This may not be the best way, but I tried it and it works. The bottom line is that when using the open() function with a relative path, such as /media/user_scans/kwa.png, then that file must be in the same directory as where the function is called. You could just find the absolute path of your image files in your computer, then append the filename to that and it will work, or you can do what I have below. import requests import json from django.shortcuts import render, get_object_or_404 from .models import Homework # ADD THESE import os from pathlib import Path def create_homework(request): if request.method == 'GET': #some GET stuff if request.method == 'POST': homework = Homework() homework.title = title homework.image = image homework.author = author homework.save() id = homework.id json_to_mathsnip(id) .... def json_to_mathsnip(id): homework = get_object_or_404(Homework, pk=id) # ADD THESE BASE_DIR = Path(__file__).resolve().parent.parent path_to_image_file = str(BASE_DIR) + homework.image.url f = open(path_to_image_file, "rb") ... some later stuff ... When you show an image in Django, or you access its url, it is a relative path, but the root is the url root, localhost:8000/, and the image url is appended to that, and Django manages to find the file (how exactly, I don't know). But the image file is in your computer, and that is what you want to open.

Related questions

0 votes
    I have the string looks like this """PID TTY TIME CMD 1 ? 00:00:01 systemd 2 ? 00:00:00 kthreadd ... related and not with pandas Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    In the Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne/!= ... (a=true, x__gt=5) Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have deployed a Django project on Ubuntu 16.04 with Nginx and Gunicorn. I have gotten everything, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    tl;dr How to autofill an editable form with information stored in database Hey, Im creating a profile page ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am a beginner at Django and I am attempting to create a custom table using a html template called ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    So, I started learning to code in Python and later Django. The first time it was hard looking at tracebacks and ... your Django code? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is Django an important source to learn? Is it important in future? Can I grow with it? So questions: Can ... can work on Django? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    What type of information is not accessible to the citizen as per RTI? 1) The particulars of its ... including amounts allocated Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    import re text = 'The quick. black n brown? fox jumps*over the lazy dog.' print(re.split('; | ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Hello I am quite new to pygame and I am trying to make an intro for a game where the user hovers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to install some machine learning libraries on a new Windows laptop (I usually have Mac laptops ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...