Django runserver - can't serve video files - django

When running Django in local mode using the runserver command video files are not loaded in the browser properly. I get the error
An error occurred trying to load the resource.
All other static files serve fine such as images, javascript and css.

I have found the answer finally, Django's runserver does not support byte range requests. See this thread:
https://groups.google.com/forum/#!msg/django-developers/NZ1qTkZ6vok/fhdz7rTtL1EJ
And this ticket:
https://code.djangoproject.com/ticket/22479

Try running
python manage.py collectstatic
Of course you must have STATIC_ROOT and STATICFILES_DIRS set properly in your settings file.

Related

Django Admin Page is looking very Ugly(without CSS I think)

Recently I hosted my django website on EC2 ubuntu Instance with nginx server on AWS. When I open my admin page of this website IT is looking very ugly and there is not css but on local server 127.0.0.0:8000 it works fine.
I also inspect on browser console, It is giving this error:
GEThttp://jassem.in/static/admin/css/dashboard.css
[HTTP/1.1 404 Not Found 51ms]
The resource from “http://jassem.in/static/admin/css/nav_sidebar.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/base.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/js/nav_sidebar.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/responsive.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/dashboard.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
How can I get back my previous django admin page. FYI I am a newbie on web technology and Django
if the problem apply on all the pages of your project try collectstatic,
Django provides a command to collect static files from all applications
into a single location. This simplifies the setup for serving static files in production.
Open the shell and run the following command:
python manage.py collectstatic
You will see this output:
165 static files copied to '/yourproject/static'.
Files located under the static/ directory of each application present in the
INSTALLED_APPS setting have been copied to the global /yourproject/static/ project
directory.
then, edit the config/nginx.conf file and add:
location /static/ {alias /home/projects/yourproject/static/;}
location /media/ {alias /home/projects/yourproject/media/;}
after the lines:
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass yourproject;
}
replace the /home/projects/yourproject/ path with the absolute path
to your project directory
You need to run the collectstatic command:
SSH into your EC2 instance
Run python manage.py collectstatic
Assuming you have set up your static files correctly, it should work after that.
Try running this command
python manage.py collectstatic
if it does not work, try this then
python manage.py collectstatic --no-input
This was my first deployment and I tried many things. Finally, this is what worked for me:
Running python manage.py collectstatic
Finding the nginx.conf file and within the server{} block, adding: location /static/ {alias /home/projects/yourproject/static/;} then runinng sudo service nginx restart from the bash terminal
In my settings.py file adding: STATIC_ROOT = os.path.join(BASE_DIR, 'static') and STATIC_URL = '/static/'
just turn on your DEBUG mode in the setting.py file and it will work just fine.
DEBUG = True

Divio Live server not serving the static files when DEBUG=False

I am running a website in Divio Cloud. My Test server serves the static files fine but my Live server gives 404. When I switch DEBUG to True on the Live server it starts serving the static files fine.
When the Live server starts, it has this in the log:
[uwsgi-static] added mapping for /static/ => /app/static_collected
Shouldn't that take care of serving the static files correctly?
--edit--
More info. I actually didn't have any "normal" static files. I added a static file using {% static %} and it is working! The files that are not working are collected with django-sass-processor and django-compressor. I have in settings.py:
STATICFILES_FINDERS.extend([
'compressor.finders.CompressorFinder',
])
Shouldn't this only affect when the files are collected?
I think the issue is most likely in the fact that you are using Django Compressor. There are various different ways in which to use Django Compressor.
In its documentation, please see Offline Compression. This is what you need for it to work on Divio Cloud.
For future reference:
In Divio environment to get the compressor based processors (in this case it was django-sass-processor) to work is add something along following lines in the end of the Dockerfile:
RUN DJANGO_MODE=build python manage.py compilescss
# <STATIC>
RUN DJANGO_MODE=build python manage.py collectstatic --noinput
# </STATIC>
# Remove the css files in development environments (in Live they
# are already collected)
RUN DJANGO_MODE=build python manage.py compilescss --delete-files

Apache will not serve Django static files

So I've seen alot about Apache not serving Django admin static files, but for some reason, Apache is not serving any static files. It understands and finds the templates, but no images, css, or javascript is loaded.
EDIT 2: Updated the two files to show new settings
EDIT: I added the STATIC_ROOT and I was able to collectstatic files, but it still doesn't serve them after server restart.
I've tried ./manage.py collectstatic and get this error:
ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without
having set the STATIC_ROOT setting to a filesystem path.
My settings.py file is here:
https://github.com/rchurch4/stackquestions/blob/master/settings.py
My nxt4.com.conf file is here:
https://github.com/rchurch4/stackquestions/blob/master/nxt4.com.conf
If someone could please let me know exactly how to configure this so that Apache will serve django's static files, that would be great. I'm running Ubuntu on AWS with a mysql db. The filepath to the site on the server is: /home/ubuntu/nxt4.com/nxt4/
Thanks in advance
The error message seems quite clear: you have not set the STATIC_ROOT setting, so collectstatic does not know where to put the collected files. From the looks of your httpd.conf, it seems like it should be set to "/home/ubuntu/nxt4.com/static/"

Why won't newly installed Django app with NGINX serve static assets properly?

I have a Mac running OS X 10.9.3. I am trying to setup a Django application backed by a PostgreSQL database served by gunicorn, with static assets served by NGINX. I'm an old hand at Django with MySQL running with the developement server (manage.py runserver). But I'm new to setting it up with virtualenv, gunicorn and NGINX. So I'm following the instructions here.
My Django Project is being served successfully at localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited localhost:3026/admin/
I have included a screenshot below.
Why does this admin page look so ugly? It lacks the neccessary graphical interface and css that it is supposed to have? It looks like NGINX is not properly serving up those static assets. How can I troubleshoot and fix this issue?
EDIT:
After I posted this question, I did python manage.py collectstatic. That went and successfully copied all the static files to where they were supposed to (I think?) live in /opt/myenv/static. You can see the output of that command here. I then re-started gunicorn and nginx. I thought that would fix it. But unfortunately it didn't. The issue remains. In my Django settings.py file, I have configured the STATIC variables as follows:
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Try run command,
python manage.py collectstatic
If the commands executes successfuly, the static file would be generated in your project path, and then if you config the right static path, the web page will be correct.

Heroku collectstatic not run during deployment

I have a django app that I am successfully deplying to heroku. When I dry-run the collectstatic command locally everything works fine.
python manage.py collectstatic --dry-run --noinput
....
Pretending to copy '/Users/hari/.virtualenvs/bsc2/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/ordering.js'
Pretending to copy '/Users/hari/.virtualenvs/bsc2/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js'
71 static files copied.
Despite this ..my django admin staticfiles do not get used and I get a bare-bones django admin site on heroku with Debug set to False.
If I set Debug to True I get a "rich" admin site on heroku. With Debug set to True or False "git push heroku master " command terminal output does not have anything about collecting staticfiles.
I tried the example "helloworld" application that uses gunicorn from Heroku and that did display the "collecting static" messages.I also tried inserting this code snippet into my urls.py. But that too does not help.
from django.conf import settings
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
Next, I tried adding the following to my heroku config
heroku config:add DISABLE_COLLECTSTATIC=0
But that too did not show my django admin site with all the styles.
Finally I tried switching to gunicorn with my Procfile and that also did not show the admin styles. Only setting Debug=True works to show my admin styles.
I tried this with Django 1.4.2 and 1.5.1 on Heroku and neither is showing me a "normal" admin site. Is there any way out to have my admin files on heroku without going the S3 route.
command terminal output does not have anything about collecting staticfiles.
Looking at heroku-buildpack-python:bin/steps/collectstatic it seems that it tries to do a collectstatic --dry-run --noinput and pipes it's output to /dev/null before displaying the -----> Collecting static files message. This means that if there is an error there that is not present on your local box, you will never see the error on heroku : it will silently fail. (The best kind of failure ;)
have you tried running a one-off worker to test out the collectstatic command and see if it's a problem in their environment?
heroku run python manage.py collectstatic --dry-run --noinput
If this fails, it will give you an error or traceback to look into and further diagnose the issue.
Check this out Django and Static Assets. It seems to be updated recently and you can serve static files nicely using this dj-static package.
Try these three things:
Create this Heroku config variable: DJANGO_SETTINGS_MODULE with a
value of myapp.settings.prod--or as appropriate for your Heroku settings file
Use Whitenoise as described in the Heroku docs:
https://devcenter.heroku.com/articles/django-assets
Check it in and redeploy your dyno: git push heroku master
I found I was missing the first item, the DJANGO_SETTINGS_MODULE" e.g., Command line collectstatic would work but that didn't matter b/c it was an ephemeral dyno
throwing it out there, since dumb mistakes happen:
I spent much time trying to figure out why my module wasn't found on heroku when it was working fine locally, only to realize it was ignored by an entry into .slugignore