How to use pdf.js with django - 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}),

Related

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

Django how to display img in template by setting MEDIA_ROOT

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

Can't cope with images uploaded by a user during development

Django 1.9.7
Could you help me cope with user uploaded images. I have managed to save images.
But I can't show them. So far this is all about development stage (not production).
The bottommost code sample shows the html when I execute "View page source" in Chrome. This "src="/home/michael/workspace/..." is absolute path. It will work if I create such html and open it in the browser without a webserver.
But whey I run the Django dev server, the image doesn't show.
Could you give me a kick here.
/pharchive/pharchive/settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, '../media/')
MEDIA_URL = os.path.join(BASE_DIR, '../media/')
/pharchive/pharchive/urls.py
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
/pharchive/masterdocument/models.py
class Image(AbstractDocument):
image = models.ImageField(upload_to='images/%Y/%m/%d')
/pharchive/masterdocument/views.py
class ImageDetailView(DetailView):
model = Image
/pharchive/masterdocument/templates/masterdocument/image_detail.html
<html>
<img src="{{ object.image.url }}"/>
</html>
view-source:http://localhost:8000/images/6/
<html>
<img src="/home/michael/workspace/pharchive/media/images/2016/06/29/Screenshot_from_2016-02-23_205205.png"/>
</html>
MEDIA_URL should be the root URL for uploaded media, for example:
MEDIA_URL = '/media/'
You have set it to the path the the folder where uploaded media are copied.
Edit: as requested more information on how that works.
What you are trying to do is store an image file submitted by a user, and then serve it to other users. To store the file, you need to specify a location on the filesystem where to store it, in your case:
/home/michael/workspace/pharchive/media/images/2016/06/29/Screenshot_from_2016-02-23_205205.png
Django builds this path by concatenating these parts:
the MEDIA_ROOT
the ImageField's interpolated upload_to attribute, here 'images/%Y/%m/%d' interpolated to images/2016/06/29
the file name, here Screenshot_from_2016-02-23_205205.png.
Django also stores in the database the path to the file, relative to the MEDIA_ROOT, in your case images/2016/06/29/Screenshot_from_2016-02-23_205205.png
Now, to serve the stored file to your users, it first needs to be accessible through a URL, this URL is built by concatenating the MEDIA_URL setting to the path stored in the database (maybe modified to make it URL compatible), here it gives /media/images/2016/06/29/Screenshot_from_2016-02-23_205205.png.
The last step is to actually serve the file when the previously constructed URL is accessed, typically it will not be done the same way in development and in production.
In development, the Django devserver will be used to serve the file, which is why you add a url pattern when DEBUG is true. That url pattern will map any URL starting with the MEDIA_URL (/media/) to a view that will read the stored file and return its content.
In production you will use a dedicated web server to serve uploaded files, for performance reasons.

Django settings issue

Can any one tell me what is the issue in finding the media directory.In the templates when i say /media/images/logo.jpg It is unable to find the /media directory
The /media directory is in /home/project/myproject
Can any one tell me how to resolve this issue
I have the follwoing in my settings.py file
MEDIA_ROOT = '/home/project/myproject/media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
The following is in myproject.wsgi file
sys.path = ['/home/project/lib/python2.6/django/', '/home/project/lib/python2.6','/usr/local/lib/python2.6/site-packages','/home/project/myproject'] + sys.path
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = WSGIHandler()
But you've set MEDIA_URL to ''! So why are you looking for images in \media?
If you want images to be in \media, you should set MEDIA_URL to that. Although you'll need to set ADMIN_MEDIA_PREFIX to something else.
MEDIA_ROOT is where files served at MEDIA_URL are resolved from. Users that are requesting static files can't hit your file path (MEDIA_ROOT), but they can request resources from a URL (MEDIA_URL). You need to define the MEDIA_URL, and you need to ensure that it is different to ADMIN_MEDIA_PREFIX.

Django static media files are not coming through

I cannot, for the life of me, get my static media files to come through on my local test machine in one particular project, though I've had no problem with a few other Django projects.
I'm using Django 1.1.1 and a Windows XP environment.
My settings.py looks like this for the media-hosting stuff:
ROOT_PATH = os.path.dirname(__file__)
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(ROOT_PATH, 'site_media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/site_media/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
And it's looking in here:
http://127.0.0.1:8000/site_media/schedule/img/add.png
And the file is in the site directory along the lines of this:
c:\django\project_file\site_media\schedule\img\add.png
But for some reason the image still ain't showing up.
Any advice?
Your media root should be fully specified; define your MEDIA_ROOT as follows.
ROOT_PATH = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(ROOT_PATH, 'site_media')
Did you read this documentation about static files; you should also add extra route configuration to your urls.py to make sure that requests to the static content are handled correctly.
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),