Embed PDF media file in Django template - django

The question is that I want to embed a PDF file through <embed>, <iframe> tag, or similar in a Django template using {{ object.file.url }}, which is presumed to be a file saved in the /MEDIA/ folder by a Django model.
The fact is that if I embed a PDF file coming from /STATIC/ folder, everything goes well. However, if I try the same with the {{ object.file.url }}, it does not work.
The curious thing is that if I simply display the {% static 'path/pdf.pdf' %} and {{ object.file.url }}, they both give me a valid link like this:
/static/img/111.pdf
/media/projects/phases-guide/111.pdf
If I try to open them separately, they work perfectly. But at the moment of embedding them in a HTML tag, it only works with the static url file.
Can someone tell me any idea why this is happening?
Anyway, is there any alternative to "EMBED FILE COMING FROM MEDIA FOLDER ROOT"?
I tried to enable read media files, but anyway it is not working.

The problem is that clickjack protection is being enabled on the MEDIA files that you are trying to embed.
You can see in the error console in your browser’s developer mode that ”X-Frame-Options” is set to ”Deny” for MEDIA files but not for STATIC files.
There are several ways of changing this per view using method or class decorators, but that didn’t work in my code, so I did a generic change in SETTINGS.py like this:
X_FRAME_OPTIONS = ”SAMEORIGIN”
Then it worked for me.

Related

{% extends 'NavigationEG/layout.html' %}

in flask templates, we combine HTML pages using extends, but my view(HTML) files are in diff folder. so how u can extend that? how to set a path? eg: {% extends 'NavigationEG/layout.html' %} NavigationEG is my folder but this way not work
without a folder, it works but I want to store it in one folder.
Like bergp said in this related question on stackoverflow, flask is looking for templates in the given templates folder. If you want to reference a different folder in your template, you could remove the template_folder when initlializing the app and refering to the full path in the render_template parameters.
Depending on the reason for the different folders, blueprints can have their own template_folder as well.
I hope this helps. If not, please share your file structure and parts of the code so we can help you find the source of this problem. Good luck.

Image in static couldnt be found without using CSS under Django frame

I want to input an image in the .html so I put the image in the static files and I don't want to apply CSS. But it says it can't find that pic I wanna use.
In the .html file, I wrote like this
<img src="images/background.jpg alt="It is an img">
So, can anybody tell me what to do or anything I should change in other files like settings.py?
My files context is what the image shows.
Please add some code for better understanding of your question, and you can add image directly in your question without sharing external links.
For static file you can use,
{% load static %}
<img src="{% static 'your image location' %}" alt=image>
# in your case location is 'lemonade/images/background.jpg'
Here is the official document,
Django Static Files

(Django) How to include template file from app into base.html in templates

I am building a site in Django CMS. In my templates directory for the project is base.html.
I am writing an app "was_this_helpful" to add a dialog box on some pages for users to give feedback. I want to include a file from was_this_helpful/templates into base.html but it says the file does not exist.
{% include 'was_this_helpful/dialog.html' %}
My file structure look like this:
- was_this_helpful
- templates
- was_this_helpful
- dialog2.html
- dialog.html
- required app files
I read somewhere that sometimes template files need to be another level deeper in templates to be found which is why I made the dialog2.html but still it's not working. I do not understand how to accomplish this. Based on what I've read it should work. Is it different because I'm not in another app, just the templates directory?
Without knowing more it's hard to tell if it's a simple solution or not.
The way you have your code written, there is not a was_this_helpful/dialog.html - you only have a dialog2.html inside your was_this_helpful so was_this_helpful/dialog2.html would be the reference path.
I've always created another folder inside my templates folder with the name of the directory above my templates folder. Just like you have with your was_this_helpful second directory. I find that this makes it much easier to extend base.html files.
You can always do it absolutely too by two periods before the path call, so ../was_this_helpful/templates/dialog.html
If you don't have luck with that either, there is an {% extends %} method as well which might accomplish what you're trying to do as well.
Good luck!

Taking all javascript from html to page-specific js file

What bit bothers me about django, is that I see in many examples that raw javascript is included in html with <script> tag. I would like to have it in independent files which are included in every page in <head> tag so that html stays clean. So that I will call something like {% add_jscript %}some js code{% endaddjsscript %} anywhere in the template to add js code. After all processing when the page is generated and it will dynamically collects all portions of added js code from processed templates and serve it as one js file.
Some app already does this or am I forced to do this on my own ?
I use django-sekizai (https://github.com/ojii/django-sekizai/) for this kind of thing. If I understand you correctly, I believe that is what you are looking for.
I know I'm a bit late to the party, but another option you could try (shameless plug) is a django app i've been working on which will allow you to inject django variables directly into external javascript files, a la Require.js
django-js-variable-injector

Why is my Django app not displaying dynamic images when the path is correct?

My Django 1.1 app uses dynamic images.
I'm confused about why the path generated from my template tag:
{{image_product.photo.path}}
looks correct, but does not display the requested image.
This generates a path that works:
src='/media/{{image_product.photo}}' => <img src='/media/lcdtvs/product1.jpg'>
This DOES NOT work:
src='{{image_product.photo.path}}' => <img src='/Users/Bryan/work/review_app/media/lcdtvs/product1.jpg'>
I checked to confirm that the Absolute path generated from MEDIA_ROOT is correct on my computer and it works fine.
Why would the image not display correctly?
Two things to keep in mind:
What you want is the {{image_field.url}} method (not the path).
If it still is 404, either you need to setup your server correctly, or if you are using the development server you need to enable it to server static files.
Not familiar with Django, but I'd guess you need to have file:///Users/... in the second snippet.