Using an embedded Iframe in Django? - django

How do I use an Iframe in Django. Currently its searching for the src url in my own files. How do I tell it to not look in my files but to look at the url?
Currently I'm storing the embed code minus the tag in a database and then trying to dynamically generate it in my template.
games.embed = 'allowtransparency="true" width="485" height="402" src="//scratch.mit.edu/projects/embed/82384372/?autostart=false" frameborder="0" allowfullscreen'
{% extends 'code_games/base.html' %}
{% block content %}
<div class="game">
{{games.title|linebreaks}}
<iframe '{{ games.embed }}'></iframe>
</div>
{% endblock %}
The iframe itself shows up on my page but the contents of it don't.
The request URL per the error:
Request URL: http://chacemcguyer.pythonanywhere.com/games/1/%22/scratch.mit.edu/projects/embed/82384372/?autostart=false%22
You can see that its searching for the url in my site. How do I get around that?
The error also says:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
Then it shows all of my urls from settings.py

I found a solution!
What I had to do was make sure that the iframe content which was coming from my database was |safe
I changed:
<iframe src='{{ games.embed }}'></iframe>
to
<iframe src='{{ games.embed|safe }}'></iframe>

Related

External URL into a iframe to embed an external url within Django

I would like to embed a pptx that is uploaded into a folder in OneDrive within a iframe tag in a Django template. I have the urls stored in a model and saved into the SQLite database. In this sense, in the views.py file, I have the following code:
context = {
'selectedunit': selectedunit,
'url_to_be_inserted': MyModel.objects.filter(unit=selectedunit)
}
return render(request, 'web.html', context)
The code for web.html is very easy:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="container col-lg-8">
<div class="center">
<iframe class="center" src={{ url_to_be_inserted.url }} width="100%" height="300%" frameborder="0"/>
</div>
</div>
{% endblock %}
The result is the snapshot below:
While, I would like to embed the ppt into the web site. If I directly insert the URL, instead of using the context variable, it works. That is to say:
<iframe class="center" src="https://..." width="100%" height="300%" frameborder="0"/>
In this case, the result is as follows (ppt embedded into the Web site):
The reason why doing in this sense is because, depending on the selectedunit variable, I would like to address a different pptx with a different URL and I would like to dynamically change the pointer (as you see above by filtering the model).
How could I solve it?
Many thanks in advance

problem with images on a custom 404 page, django

I am trying to make custom 404 page by adding an image to the page. Problem is that image is not displaying on a page. This image is accessible from any other page in each app in DEBUG TRUE or FALSE mode, no difference, except 404 page where it represented as a just an empty frame.
Image is placed in app/static/app root. Tried to place it is main static root -no difference.
# urls.py
handler403 = curry(permission_denied, exception=Exception('Permission Denied'),
template_name='errors/403.html')
handler404 = curry(page_not_found, exception=Exception('Page not Found'),
template_name='errors/404.html')
handler 403 and handler 404 works fine, except images failure.
# template
<!-- templates/errors/404.html -->
{% extends "base.html" %}
{% block title %} error 404 {% endblock %}
{% load static %}
{% block content %}
<img src="{% static "appname/404.png" %}" alt="404">
{% endblock content %}
Problem is only on 404/403 pages. Everywhere else -no problems at all. Text itself displays normally.
Question is what should I check in terms of possible pitfalls I possible keeping out of scope ?
thanks in advance
p.s. tried
<img src="{% get_static_prefix %}404.png" >
still no effect
Solution is to run server in the following mode:
manage.py runserver --insecure

Using Django with Bootstrap Dynamic Tabs

When I use Bootstrap Dynamic Tabs it requires me to use ids in the list anchor tags. Whereas I would rather use a url to a django page like {% url 'incidents:report' %}. How can I get it to use the django url and still maintain functionality?
Bootstrap dynamic tabs work by hiding/displaying inline content within specific divs. You will need to get that content loaded inline, either through some sort of script (like an AJAX request), or an iframe:
<div id="menu1" class="tab-pane fade">
<iframe style="border:none" src="{% url 'incidents:report' %}"></iframe>
</div>

Can't Embed YouTube Link Within Django Template Tag, Twitter Bootstrap

I'm currently attempting to embed a user-submitted YouTube link via a Django form within a Twitter Bootstrap layout.
The space for the video is appearing and the source code reflects the correct information and link, but neither the video nor the player appears. I'm also using the "flex-video" class from this link for a responsive layout http://poslavsky.com/blog/2012/04/01/responsive-video-in-twitter-bootstrap/ but it doesn't work when that class is changed to another name such as "video" either.
This is the code:
<div class="row">
<div class="span12">
<span>{{story.author}}</span><br>
<span>{{story.zip_code}}</span><br>
<span>{{story.date}}</span><br>
<p>{{story.copy}}</p>
<div class="image">
{% if story.pic %}
<img src="{{ story.pic.url }}" alt="some_image_alt_text" />
{% endif %}
</div>
<div class="flex-video">
{% if story.video %}
<p> <iframe width="460" height="250" src="{{ story.video}}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</p>
{% endif %}
</div>
</div>
Any insight greatly appreciated.
I guess {{story.video}} gives Youtube video Url something similar to this http://youtube.com/watch?v=ASO_ypdnsQ which is direct youtube url, and not embed url.
Embed URL for same video is different than direct url, like this http://youtube.com/embed/....
I created a custom template tag, here is how.
import urlparse
...
#register.inclusion_tag('video_embed.html', takes_context=True)
def youtube_embed(context, url):
url_data = urlparse.urlparse(url)
query = urlparse.parse_qs(url_data.query)
video_id = query["v"][0]
context['embed_url'] = ('http://youtube.com/embed/%s' % video_id)
return context
Then, in templates just load the tag, and pass the youtube url. It will give embed url from normal url.

Problem embedding youtube video's with with django template

I have a django template that displays a list of objects with youtube videos:
{% for obj in objs %}
<h1>{{ obj.name }}</h1>
<iframe width="425" height="349" src="{{ obj.video}}" frameborder="0" allowfullscreen=""></iframe>
{% endfor %}
obj.video is stord as a urlField. When I load the page chrome console gives me the error refused to display document because display forbidden by x-frame-options.
The problem persists if I replace {{ obj.video }} with a manually written youtube embed url such as http://youtu.be/zzfQwXEqYaI. However, if I replace it with something like www.google.com the iframes will load.
Try embedding the video like with url like:
http://www.youtube.com/embed/zzfQwXEqYaI
I guess its some kind of protection from youtube