I'm trying to create a home media server using Django. For this I want it to be in such a way that my media files are stored in an external USB. Now from my code when I try to load the video, it doesn't work. I even tried hard coding a path to see if it works. When I run through Django, It doesn't work but when I directly open the HTML in chrome It works perfectly.
Placeholder Template Code:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<video width="640" height="480" controls autoplay>
<source src="E:/Programming\Projects\Youtube Downloader\Ariana Grande - One Last Time (Official).mp4" type="video/mp4">
</video>
<p><b>Note:</b> The autoplay attribute will not work on some mobile devices.</p>
</body>
</html>
Actual Template:
{% extends 'MediaPlayer/layout.html' %}
{% block MainContent %}
{{ video_source}}
<video width="640" height="360">
<source src="E:/OneLastTime.mp4" type="video/mp4">
</video>
<p><b>Note:</b> The autoplay attribute will not work on some mobile devices.</p>
{% endblock %}
{% block PageScripts %}
{% endblock %}
View that calls the template:
def select_video_page(request, video_id):
file_path = FILE_SCANNER.files["video"][video_id]
context = {
"video_source": file_path,
"title": '.'.join(file_path.split("\\")[-1].split(".")[:-1])
}
return render(request, "MediaPlayer/selectvideopage.html", context)
Everything on the page works perfectly fine, Template loads and everything. Only issue i'm facing is with the video file. Almost all the solutions I have found requires storing the Media in a media directory under Django, But this would defeat the purpose of my project.
If you post your settings.py it will be helpful in determining your problem.
You are trying to serve static media from your Django project. You need to properly specify which directory you want to use as your media root, i.e. your USB drive. Additionally I don't see any non-trivial way to enable the django app to support hot-plugging of the USB drive, this may also lead to other problems if the media is disconnected.
This HTML indicates that you are not serving the files from the Media Server (unless you have defined your MEDIA_URL as E: in settings.py)
Even if you got this url to render a video, it would not work for any other network device unless the file is being served by django's media server given the filesystem path below.
<source src="E:/OneLastTime.mp4" type="video/mp4">
django static file serving guide
You will most likely need to use python's OS module to navigate to the directory on your usb drive and then declare this directory as your media root for django to serve files from it. There may be built in security features that prevent serving files from outside the root of the project.
Related
This question regards development server.
I have an audio file audio1.wav sitting in my MEDIA_ROOT/audio folder.
How can I attach such element to an <audio> tag in the template?
<audio src="?"></audio>
Do I need to construct an absolute URL for the file like http://127.0.0.1:8000/media/audio/audio1.wav (given MEDIA_URL is set to /media/)?
If so, how? Or perhaps there is a better way that will also cover for variable audio file name?
You will need to use get_media_prefix to get media url:
{% load static %}
<audio src="{% get_media_prefix %}audio/audio1.wav"></audio>
I'm trying to use django's static templatetag to display an SVG, but it doesn't seem to recognize the SVG as a valid image url. This is what I currently have:
settings.py
import mimetypes
mimetypes.add_type("images/svg+xml", ".svg", True)
landing.html
{% load staticfiles %}
<img src="{% static 'images/right-arrow.svg' %}" />
At least in my view.py, it recognizes the SVG mimetype:
views.py
print(mimetypes.guess_type(static('images/right-arrow.svg')))
# returns ('images/svg+xml', None)
The SVG does display in a non-django page, and it will download the SVG if I try to open the SVG path in a new browser tab.
I'm currently using python 3.4 and django 1.8.4.
I found the issue. In settings.py, it should be mimetypes.add_type('image/svg+xml', '.svg', True). image should be singular.
I faced a similar issue.I would recommend you to use :
src="{{ STATIC_URL }} images/right-arrow.svg" instead of src="{% static 'images/right-arrow.svg' %}"
svg format might not always identify django's method of obtaining staticfile contents.Hope this helps :)
Add this in your settings.py file.
import mimetypes
mimetypes.add_type("image/svg+xml", ".svg", True)
mimetypes.add_type("image/svg+xml", ".svgz", True)
In your cases you have added images in add_type which should be singular (image).
You are loading staticfiles and using static?
This is wrong.
Try changing {% load staticfiles %} <img src="{% static 'images/right-arrow.svg' %}" /> to
{% load static %} <img src="{% static 'images/right-arrow.svg' %}" /> and you also need to consider which app you should find your static files.
I am trying to set the background image using jinja2. I have managed to serve my file from my dev server and can post the image in the document, but I want it to form a nice background with everything else on top.
As an example, I tried this :
{% extends "layout.html" %}
{% block body %}
{% if current_user.is_authenticated %}
{% if commPageData.background_image %}
<body background="{{'/images/'+commPageData.background_image}}">
{% else %}
<body>
{% endif %}
A thing
</body>
I have a css file and a layout file that gives the default behaviour for a template. How can I have a nice background image which I am serving?
You seem to be trying to tell the front end to use application routing without passing it the right commands. I'm assuming you're using Flask. Really, there are two ways (that I can think of) to slice this bread, and it just depends on you. The first is just using standard html and not embedding python to route the app to look for the file, and looks like:
<body background="/path/to/image.jpg">
But, if you want to take advantage of the framework and the template, then you should use the built in method url_for
It looks something like this:
<body background="{{ url_for('static', filename=commPageData.background_image) }}">
'static' being the directory in the application where this image lives.
Now, I'm not sure where commPageData.background_image is coming from in your app. My code snip assumes it is being served as a value it will recognize if passed back to the logic, if that makes sense, and it needs to be where you tell the url_for method looks for it, even when dynamically generated. If you actually have a specific image you want to render as the background, it needs to be specified appropriately:
<body background="{{ url_for('static', filename='image.jpg') }}">
Of course, has been deprecated in HTML5 and may not render appropriately in many browser. It is much preferred to use CSS instead with
<body style="background:url({{'images/'+commPageData.background_image}});">
or put it directly in your CSS file
I'm use flask-assets for bundling, minification and versioning (making sure that when we change a CSS or JS file, the browser loads the new version, instead of what's in its cache... but of course we want it to load from the cache subsequently).
Since the site only has a few pages and they all use different resources, I've defined the bundles in the templates themselves, as described under Templates Only in the docs:
{% assets filters="jsmin", output="gen/our-page_packed.js",
"blah.js", "yadda.js", "rhubarb.js", "wibble.js" %}
<script src="{{ ASSET_URL }}"></script>
{% endassets %}
{% assets filters="cssmin", output="gen/our-page_packed.css",
"css/foo.css", "css/bar.css" %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
ASSETS_DEBUG is False in production, and the site is not localised, nor served through a CDN or with S3.
The problem is, when we push to production, the bundles initially apparently aren't being created. The pages lack CSS and Javascript, and the apache error log contains errors like this:
File does not exist:
/srv/our-client/our-client/static/gen/our-page_packed.css, referer:
https://app.our-client.com/quux/123/xyz/
After we reload the page a number of times, over the course of a minute or two, it all starts working. We occasionally hear customer complaints about what sounds like missing CSS or JS, but it doesn't seem to persist and it's not clear it's a related issue.
I'm afraid I'm far from a Flask expert (the site was created by another developer; I added flask-assets) but it seems a fairly straightforward setup. Is there something I can do to make sure the bundle files get created early?
I am new to django. My project folder contain media folder. It contain some images.
How can I display this images in template of another app?
Path : mysite/media/blog/templates/blog/details.html
details.html
{{<img src="media/image_2.jpg">}}
But it does not display anything..
You should set-up the STATIC folder and to configure the media files.
The full reference: https://docs.djangoproject.com/en/dev/howto/static-files/ and also
https://docs.djangoproject.com/en/1.4/howto/static-files/.
However you should use something like this (once you configured properly the STATIC files) :
<img src="{% static "media/myexample.jpg" %}"/>
Edit:
Before using static tag, You must load specific tags incluing on top of template code: {% load staticfiles %} : Referring to static files in templates