Video not playing on Django when debug=False - django

Videos are not playing when I turn DEBUG to False on Django, but on DEBUG=True, everything is fine.
The task is simple, the app receives youtube shorts link, downloads it to the MEDIA_ROOT (which declared in settings.py as BASE_DIR / 'media'). And it returns rendered template video.html, on template:
<video height="450" autoplay="autoplay" controls style=""> <br> <source src="{{ MEDIA_URL }}{{ file }}" type="{{ mime_type }}">
on html : <video height="450" autoplay="autoplay" controls style="">
<source src="/media/filename.mp4" type="video/mp4">
views:
{"MEDIA_URL": MEDIA_URL, "file": str(video.video).split('/')[-1],}
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = Path(__file__).resolve().parent.parent / 'media'
As I said when debug mode turned on everything works perfect, but I turned it off video is not playing. Did I forget something? Or any mistake I have? how to solve this problem?
P.S. I am pretty new on django, I searched for many sources, tried them all, but I couldn't solve this problem

Django does not support file serving in production (have a look here). The helper function "+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) " works only in debug mode. Therefore it cannot read the file from media.
If you want to use the file in production with debug=False, try to switch to another storage backend or serve it from elsewhere.

Related

Adding image as a background for a django template

I have checked a links for how to set an image as background for a div. I have tried various ways. But still am going wrong somewhere and would need some guidance...
Below is my directory structure:
frontend
/frontend (django project)
settings.py
.
.
/webApp (django app)
/templates
/static
img1.jpg
home.html
In my home.html I have the following line,
{% load static %}
<div style="background-image: url({% static "/img1.jpg"%}); height: 650px; width: 1920px;">
In settings.py I have,
STATIC_URL = '/static/'
But the image does not appear when the page is displayed. I get the following error.
"GET /static/img1.jpg HTTP/1.1" 404 1803
My directory structure was different and have searched a few questions before to fit my need. I still am getting confused with the urls and assigning url for the background image. Also i want it as a url and not as href to set the background-image.
How do I go about this?
Try to specify STATICFILES_DIRS in settings.py
STATICFILES_DIRS = (
"<absolute path to static folder>",
)
I made a few changes... I shifted the static folder to where my settings.py is present... And remove the / before img1.jpg in the html file... Finally it's working now...
try add but with proper path:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

Link to a django static file doesnt work

In my dev. environment there is a /static/ folder, which atm. stores some images for the web-site.
My INSTALLED_APPS variable in settings.py does contain the
django.contrib.staticfiles app and
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'),)
My urlpatterns variable in urls.py has this
urlpatterns+= static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
Finally, in my template.html file i'm iterating through my cardset objects and am trying to provide an image for each object like this
<img src=" {{STATIC_URL}}/images/{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
Now, the problem is:
a.) the {{STATIC_URL}} resolves as an empty string.
b.) i think i cannot use the static tag here because the variable {{cardset.image}} would not work well inside of the django tamplate {% ... %} with the static tag.
Could you please advice on what I should try doing here?
It seems to me you can still use the static tag. To build your string without using an image field, your link just needs tweaking slightly:
<img src="{% static "images/" %}{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
which should render in html correctly. Depending what your {{cardset.image}} looks like you may be able to get rid of the images/ part in the middle.
Remember you also need to include {% load staticfiles %} at the top of your template.
Since cardset.image is a string, your assumption b is false; tags work perfectly well with variables:
{% static cardset.image %}
(The reason {{ STATIC_URL }} wasn't working was presumably because you haven't passed it to the template; it's not present automatically, it's just a standard template variable.)

Django CSS & Static Media Not Loading

So I've been hitting my head against the wall on this for the last hour and can't seem to figure out why none of the static media (CSS, Images, JS etc) when my template is rendered.
Could someone please help me find out why adjustments I need to make? Below are snippets from Settings.py, Index.html and stylesheet please let me know if more is needed.
My static files are located in the following directory:
/djangoproject/website/static
Settings.py - Located /djangoproject/djangoprojectname/
STATIC_ROOT = os.path.normpath(os.path.join(PROJECT_ROOT,
"/static/"))
STATIC_URL = '../website/static/'
Here's a snippet from my index.html that is supposed to be calling the css style sheet with {{ STATIC_URL }}
Index.html - Location /djangoproject/website/templates/
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">
Location of CSS StyleSheet
style.css - Location /djangoproject/website/static/css/
From the Django docs:
If {{ STATIC_URL }} isn't working in your template, you're probably
not using RequestContext when rendering the template.
As a brief refresher, context processors add variables into the
contexts of every template. However, context processors require that
you use RequestContext when rendering templates. This happens
automatically if you're using a generic view, but in views written by
hand you'll need to explicitly use RequestContext To see how that
works, and to read more details, check out Subclassing Context:RequestContext.
It seems to me that you are setting STATIC_URL to a path, when it should be set to, well, a URL. You need to set this to the web address of the folder that contains your css files, for example:
STATIC_URL = 'http://mydomain.com/static_files/'
Try to find your CSS file online by typing the address you expect it to be into your browser. Once you find the CSS file this way, just copy the root URL that got you there.

Getting static files accessible on Django

I'm using Django 1.4
My static files don't seem to be running or {{ STATIC_URL }} is wrong in the html file.
In the settings, I have the static files loaded:
STATICFILES_DIRS = (
'C:/Users/dtc/Documents/Eclipse/java_applet/applet',
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
In url.py I have:
urlpatterns += staticfiles_urlpatterns()
In the html file, I have:
<html>
<title>The Hello, World Applet</title>
<img src="{{ STATIC_URL }}tets.png" />
<applet code="{{ STATIC_URL }}HelloWorldApplet.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
</html>
Now, when I runserver, I can download the static files from localhost:8000/static/file which means it should be there right? But when I load the page, neither the applet or the image is showing up. All I'm trying to do as an endgoal is to run an applet on my dev server but I can't seem to figure out why {{ STATIC_URL }} isn't working(I even added a backslash after it in case that was the reason).
Look at the RequestContext class, this is probably what you're looking for.
See this answer on SO as well.

Cannot get images to display in Django(1.3) project

I'm making a simple Django project but I cannot get any images to display in my pages.
Django documentation at https://docs.djangoproject.com/en/1.3/howto/static-files/#basic-usage states
Basic usage Put your static files somewhere that staticfiles will find
them.
By default, this means within static/ subdirectories of apps in your
INSTALLED_APPS.
Your project will probably also have static assets that aren’t tied to
a particular app. The STATICFILES_DIRS setting is a tuple of
filesystem directories to check when loading static files. It’s a
search path that is by default empty. See the STATICFILES_DIRS docs
how to extend this list of additional paths.
Additionally, see the documentation for the STATICFILES_FINDERS
setting for details on how staticfiles finds your files.
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
For local development, if you are using runserver or adding
staticfiles_urlpatterns to your URLconf, you’re done with the setup –
your static files will automatically be served at the default (for
newly created projects) STATIC_URL of /static/.
You’ll probably need to refer to these files in your templates. The
easiest method is to use the included context processor which allows
template code like:
See Referring to
static files in templates for more details, including an alternate
method using a template tag.
So I did this in settings.py:
STATICFILES_DIRS = (
'/home/abc/django/project1/media/',
)
and enabled 'django.contrib.staticfiles',
In media, I have a folder img, which has various jpg files.
In my template, I have this as one of the lines:
<img src="{{STATIC_URL}}img/{{var}}.jpg">
When I'm passing var to this template via my view.
The HTML page seems to render this tag as "<img src="img/abc.jpg"> where var="abc".
But my browser refuses to display the image. What have I done wrong?
Did you see ths part in the documentation:
If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.
Do you also use RequestContext in your view to render the template?
Here is an alternative approach:
settings.py:
import os
PROJECT_DIR = os.path.dirname(__file__) + '/../'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'media/')
STATIC_URL = '/media/'
In your template:
{% load staticfiles %}
<img src="{% static img/foo.jpg %}" />
If you need to pass a variable, use the prefix method:
{% load staticfiles %}
<img src="{% get_static_prefix %}img/{{var}}.jpg" />