Django : static files not loading - python

My django project contains a folder STATIC in which there are it's child folders css, images, js and swf. My web-site can not locate these files raising a 404 in the development server running on the terminal.
I have a settings_local.py file in which I have set the path such as (running on ubuntu 11.04)
STATIC_ROOT = '/home/zulaikha/dust.bin/my_project/static/'
STATIC_SERVE_PORT = 80
The sample settings-local.py file from the client suggest suggest so
# static root
STATIC_ROOT = 'C:/Program Files (x86)/XAMPP/htdocs/static'
STATIC_SERVE_PORT = 8080
I have seen some similar issues but all regard STATICFILES which I read was introduced in Django 1.3
Where am I wrong and how can I correct myself?

If you are indeed using django 1.2 then you must install django-staticfiles. If you do not see it in the included requirements.txt from your client then he either did not freeze it out into the file, or was not using that feature and instead was just pointing at it via apache or another production server.
Follow these instructions from the docs. Specifically the basic section:
https://docs.djangoproject.com/en/dev/howto/static-files/
You will need to add staticfiles to your installed app. You should also not need to manually add a url since the django should do it automatically with the runserver command.
Also verify that it works locally first by not running with sudo or a custom ip and port

According to your comments there are two options here:
Contact the programmer who wrote the code and ask him.
Rewrite static serving logic with
django-staticfiles (Django 1.2 is enough)
django.contrib.staticfiles and Django 1.3

In the simplest form, this is the solution to the problem. In project urls.py include the following lines
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)

Related

Django admin static files magically served

I just setup a simple Django website for development, and commented out
path('admin/', admin.site.urls),
with STATIC_URL = '/static/' in the settings.py,
When I do python manage.py runserver 8100 and goto http://localhost:8100/static/admin/css/nav_sidebar.css, I see
this static file is magically severed.
What's really going on? I have not setup the static url serving in my urls.py yet? I also do not have another static server like NGINX.
I think the static files are served by e.g. NGINX. They are put to a location where they are accessible once you execute python manage.py collectstatic , and then Django does not care about them any more, because they are taken care off by your server. They are outsourced, so to say. Its a different service that handles them. So, the /static will be remain accessible regardless of the admin URLs. What you switch on and off with the admin URLs are the pages under /admin.
http://localhost:8100/admin
Will not work anymore, but
http://localhost:8100/static/admin
will stay untouched.
So from https://docs.djangoproject.com/en/3.2/howto/static-files/, when you set DEBUG=True, Django will automatically do collectstatics and server the static file thur the URLs for you.

Why am I getting an internal server error when deploying Django with Heroku (admin page still works)?

I am trying to deploy my Django website with Django with Heroku, and it keeps showing me "Internal Server Error." None of the other answers regarding this same problem here at SO solved my problem, and I noticed that I only have this problem when I set DEBUG to False.
My heroku logs commands show me the following error:
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for '/images/posting/bike.png'
I have setup my settings the following way:
ALLOWED_HOSTS = ["stratagembetaapp.herokuapp.com"]
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
django_heroku.settings(locals())
Additionally, gunicorn, django-heroku, and whitenoise are perfectly installed. My requirements.txt and Procfile are also in order.
I have also already run the "python manage.py collectstatic" in the Heroku shell, but I still get the same result.
The error is that you are referencing a static file in your templates which doesn't exist (or isn't in the right place). In DEBUG mode this doesn't generate an error, but in production it does.
Somewhere in your templates you have a reference like this:
{% static '/images/posting/bike.png' %}
I think the error here is just that leading slash, so possibly it will work if you just use {% static 'images/posting/bike.png' %}.
when you deploy to heroku successfully but Application error or internal server error
do the following !
1 with debug=False every thing works fine in development environment
2 make sure you did not import unnecessary packages .
3 check your code very carefully
4 run python manage.py collectstatic
5 after deployed on heroku make sure to run
heroku run python manage.py migrate
6 if you deployed successfully and it does not work .the problems are definitely minor errors in your code check them on dev server with debug=False and make sure every thing worksalso in dev server with debug=False make sure to run
python manage.py collectstatic
Are you using a static file server? From your settings, it doesn't look like you are using one. Heroku won't store your static files. These tutorials can walk you through the process. Storing your files on s3 isn't free but it is super cheap (First 50 TB/ month $0.023 / GB)
https://www.codingforentrepreneurs.com/blog/s3-static-media-files-for-django/
https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/

Django Deployment Server serving Static Files with only uWSGI

im trying to get my Django App to run using only uWSGI. The project is not that big, so i would really prefer to leave nginx out.
I just can't get uWSGI to show my static files though. I've gone through the settings multiple times and can't find the problem.
I have the STATIC_URL set to 'module/static/'
STATIC_ROOT set to '/module/static_files/' (i read somewhere that they should not be the same)
and my uwsgi.ini looks like this:
[uwsgi]
module=Module.wsgi:application
master=True
http=:80
processes=4
threads=2
static-map /static= /module/static_files/
the projects file structure is set up in the following way:
-- Project:
---init.py
---settings.py
---urls.py
---wsgi.py
-- Logs
-- module
---static
---static_files
---[... module template, model, urls etc]
-manage.py
-db.sqlite3
I can run collectstatic and generate all the static files in the correct folder.
But when i run the uwsgi script, it wont work and gives me a 404 file not found for all static files.
I would really appreciate any help, I've been stuck on this for an entire week now...
(i have checked out Deployment with Django and Uwsgi
but as far as i can tell, my static-map is set correctly)
you need to add your static files as static-map=/static=/module/static_files/

Why gunicorn cannot find static files?

Running Django dev server has no problem: 'python manage.py runserver 9000'
But if use gunicorn, it complains:
'http://innovindex.com/pubmed/static/js/jquery-3.2.1.min.js '
Why gunicorn cannot find a local jquery but Django can?
The settings are:
settings.py (seems not related):
STATIC_URL = '/pubmed/static/'
in '/etc/nginx/sites-enabled/django'
location /static {
alias /home/django/innovindex/pubmed/static/;
}
And my app looks like this:
/home/django/innovindex
is where the 'manage.py' sits.
THANK YOU SO MUCH !!!
From Deploying static files in the Django documentation, you must run the collectstatic command in addition to setting the STATIC_ROOT setting.
First make sure that you're STATIC_ROOT is set to the correct path that matches your nginx config:
STATIC_ROOT = '/home/django/innovindex/pubmed/static/'
Note that this is an absolute path.
Then run:
python manage.py collectstatic
in your project directory.
This will copy all of your static files into /home/django/innovindex/pubmed/static/
I spent a lot of time trying to figure this out until I found that the below must be in your main urls.py. Just add those two lines.
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ...
urlpatterns += staticfiles_urlpatterns()

Django and django oscar

Location of django:
/usr/lib/python2.7/dist-packages/django/__init__.pyc
Location of django oscar:
/usr/local/lib/python2.7/dist-packages/oscar/__init__.pyc
My static files are not getting served properly. Above is my production setting. On my local machine, the locations are:
/usr/local/lib/python2.7/dist-packages/oscar/__init__.pyc
/usr/lib/python2.7/dist-packages/django/__init__.pyc
Could this be a possible reason for above problem?
Oscar ships its own set of static files in oscar/static/oscar When you deploy your site, you should run manage.py collectstatic so these files are also collected in your STATIC_ROOT
On DigitalOcean's Django app, your Nginx configuration is located in /etc/nginx/sites-enabled/django You may need to update the following section to point to the location of your STATIC_ROOT
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}

Categories

Resources