Django how to display img in template by setting MEDIA_ROOT - django

here is my settings
MEDIA_ROOT = 'upload/'
MEDIA_URL = '/upload/'
where the path of upload folder is PROJECT_ROOT/myapp/upload.
I successfully upload a file a.jpg to that folder, but in the render page it shows that /upload/a.jpg not found
I'm confused about the media root setting. I think it's similar to STATIC_URL,
my static url setting is like this:
STATIC_URL = '/static/
and the path of static folder is PROJECT_ROOT/myapp/static
and files like /static/a.css can ben rendered successful in web pages.

media_root is the absolute path of the folder where your media files are being stored. 'upload/' is not an absolute path.
If you're storing your media files locally (development server) it would be something like (on windows)
c:\my_app_path\media
You can hard code it or you can use the os.path.join command :
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
more informations here
Francois

Related

Django CPanel MEDIA_ROOT configuration

I have setup my Django project on CPanel.
On my python setup page on Cpanel,
I have mentioned my Application Root as "example" (where i have uploaded all files)
and my Application Url as "example.com"
settings.py contains
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py under my app contains
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my models.py contains a field
documentFile = models.FileField(upload_to='documents/files/completed/%Y/%m/%d')
Now I can see two folders on CPANEL
example (where i have uploaded all my files)
example.com (I have not made any changes to it)
I was able to visit example.com, and everything works absolutely fine.
I was able to upload to file to 'documents/files/completed/%Y/%m/%d'
When i check i can physically see that the file is created under the folder example
But i am not able to fetch it back because, when i am trying to fetch the uploaded file, its actually tyring to fetch from example.com
I am new to Django, CPanel..
Changes/Suggestion please

Django wrong imagefield.path

i have a model with an image field like this
app_icon = models.ImageField(upload_to='icons/')
the project has 'uploads' as media folder, which has subfolders, including 'icons'.
The image is correctly uploaded to the 'icons' subfolder but when i try to access it using self.app_icon.path it returns .../uploads/imagename.jpg instead of .../uploads/icons/imagename.jpg so i get file not found error. Even the url doesn't include the 'icons' subfolder
my settings:
MEDIA_URL = '/uploads/'
MEDIA_ROOT = BASE_DIR / 'uploads/'
i can't find any answer to this problem, which seems so random to me.

Django turns absolute filepaths into relative, forcing the server to be run from the project folder

I'm trying to create a website where you can add images to models, which will then be in turn loaded on the homepage. However, I've noticed that when I run my server, it tries to get images from my /home folder.
Here's my models.py:
image_directory = join(settings.STATICFILES_DIRS[0], "website/images")
class Item(models.Model):
image = models.FilePathField(path=image_directory, recursive=True)
Here's my home.html (I'm just abbreviating it, item is passed in OK:
<img src="{{ item.image }}">
I run the migrations and run the server, and I'm able to select the image in /admin. The images look like: "sub_img_folder/img.jpg"
Then I go to /home and I get the following errors:
Not Found: /home/...absolute-path-to-project.../static/website/images/sub_img_folder/img.jpg
Not Found: /home/static/website/images/sub_img_folder/img.jpg
Any help would really be appreciated.
EDIT: Here's some of my settings.py contents.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
...
MEDIA_ROOT = os.path.join(BASE_DIR + "/static/website/")
MEDIA_URL = "images/"
EDIT 2: Just to clarify, the images you add to models are already on the server. You just need to clarify which image in the admin page, hence FilePathField instead of FileField. It somehow doesn't find the image when trying to load it on the home page but it successfully shows and selects in the admin page.
EDIT:
Since you are using a FilePathField, it only stores the path on disk, not the URL. The solution would be to use the MEDIA_URL in your template to formulate the URI string, something like this:
<img src="{{ MEDIA_URL }}/{{ FILE_NAME}}">
Where MEDIA_URL is your Media URL from settings.py and FILE_NAME is the name of the file itself.
It may be better to use an actual ImageField or FileField which stores all the information you need, or just have a CharField with the file name and build the URL like above.
PREVIOUS ANSWER:
Try adding the MEDIA elements to your Django settings.py. MEDIA_ROOT and MEDIA_URL tell Django how to handle user uploaded files:
In your settings.py:
MEDIA_ROOT = "/path/to/media/folder"
MEDIA_URL = '/media/'
In your urls.py:
urlpatterns = [
.......
] += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Documentation:
https://docs.djangoproject.com/en/3.0/ref/settings/#media-root

MEDIA_ROOT: shall I hardcode the path or use os.path.join?

Django 1.11
The documentation shows that we should place, say, images to
/var/www//media/
https://docs.djangoproject.com/en/1.11/ref/settings/#media-root
But in the book "Two Scoops of Django" they recommend:
# Configuring MEDIA_ROOT
# ’DONT DO THIS! Hardcoded to just one user's preferences
MEDIA_ROOT = "/Users/pydanny/twoscoops_project/media"
And then suggest their way:
root = lambda *dirs: join(abspath(BASE_DIR), *dirs)
# Configuring MEDIA_ROOT
MEDIA_ROOT = root("media")
Do you accept the way Two Scoops of Django recommends?
In this case MEDIA_ROOT will be inside the project itself. This is bad, I think. This is somehow a mix of code and user data.
So, I like what the documentation of Django recommends: just hardcode a path to /var/www/example.com/media/.
MEDIA_ROOT is not even STATIC_ROOT. Static files are collected from inside the project. And they contain something which is more or less (CSS) a code. Whereas user uploaded files are definitely the data, not the code.
Could you comment? What your MEDIA_ROOT looks like?
Shall I use different MEDIA_ROOTS for my local machine and the production serve?
I once used following :
ENV_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(ENV_PATH, 'media/')
This will automatically detect the absolute path to the settings.py file and create media_root path.

How to use pdf.js with django

pdf.js in in my static files(as shown below):
/static/js/pdf.js
And i am able to display a pdf located in static folder(as shown below):/static/sample-pdf.pdf
but i want to be able to show a pdf located in my media folder(as a pdf should be stored as a media file in django)
media folder is in same directory as of static folder(as shown below):
project/static andproject/media
Is it possible ?
Do ask if more clarity is required.
This will display your file in the html page from the directory
<iframe src="../media/sample-pdf.pdf" id = "pdf" style="width:1500px; height:1500px;" frameborder="0"></iframe>
check console of browser to check the path is right if its not showing. Its that much simple
Try setting your MEDIA_ROOT and MEDIA_URL in your settings.py and see if it works. Open up settings.py and add this:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
# MEDIA_ROOT is used when users upload media.
MEDIA_ROOT = '/home/absolute/path/to/your/media/folder'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'
Then, you may also need to add this in your URLs.py:
from . import settings
import django.views.static
url(r'^(.*?)media/(?P<path>.*)$', django.views.static.serve,
{'document_root': settings.MEDIA_ROOT}),