I am working on Anomaly detection project working with django app. i tried to play video from local folder in django app but stuck. How to play the video?
Add this small piece of code to your template, it should do the work for you.
{% load staticfiles %}
<video width="430" height="340" controls autoplay>
<source src="{% static "video_file_name.mp4" %}" type="video/mp4"> </source> #give your video file name here
</video>
# Make sure you have copied your video file into your django projects static folder
# And specified static path in settings.py
settings.py
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )
Related
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.
I have read many posts about the topic and tried to follow all the suggestions (new to python here) but I am unable to get bootstrap to work in Django.
I have a project "myoffice" and an app "proposals" in it.
I have downloaded a css file and put it in following folder
django\bin\myoffice\proposals\static\bootstrap\bootstrap.min.css
I have made following changes to the settings.py
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
)
STATIC_URL = 'proposals/static/'
and included static files in my template like this.
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
</head>
<body>
But it is still showing view without any formatting.
I figured it out. It was actually a stupid error. I discovered, I have to restart the webserver after adding css file to the static directory and making changes to the settings.py
So I started working on this django project that I took over from someone else. There are several settings files that all inherit from a base.py settings file.
In this base.py settings file there are several static image files defined in there.
For Example:
LOGO_URL="/img/logo.png"
LOGO=STATIC_URL+LOGO_URL
This seems strange to define image locations in this way. Is this best practices? if not what is the best practice?
Thanks
More commonly you would see just the STATIC_URL defined in your settings, and then the logo would be accessed in a template with:
{% load staticfiles %}
<img src="{% static 'img/logo.png' %}" />
The docs give more detailed explanation.
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
please help bring images into the template
in django1.6 I uploaded via the form in the database image. screenshot. ( checked that the images loaded in a specific directory ) . then imported into the template file settings.py variable BASE_DIR and to all records of a table . then tried in the template to display the image as follows:
{% for entrie in all_entries_carousel %}
<a href="{{ entrie.link }}" title="{{ entrie.title }}" target="_blank">
<img src="{{ BASE_DIR }}/{{ entrie.image }}" width="300" height="200" alt="{{ entrie.title }}" />
</a>
{% endfor %}
results in images that I have not loaded and displayed.
in the source path
c:\Python33\django_projects\proj1/carousel/media/images/img1.png
please tell me how can I still display the image. Sure , there is a path without importing BASE_DIR
ps this way does not work proj1/carousel/media/images/img1.png
You need to configure your static files.
From Django docs:
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, either hardcode the url like
/static/my_app/myexample.jpg or, preferably, use the static template
tag to build the URL for the given relative path by using the
configured STATICFILES_STORAGE storage (this makes it much easier
when you want to switch to a content delivery network (CDN) for
serving static files).
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
Store your static files in a folder called static in your app. For
example my_app/static/my_app/myimage.jpg.