Django Static Files Not Being Found (debug off) - python

I cannot get Django to correctly reference my static files when debug is off. I know there are many other posts on this site about this, but none of them have fixed my issue.
My directory tree is like this:
└── project
└── app1
└── app1
├── manage.py
└── project
└── project.sock
└── projectenv
└── static
└── template
In my settings.py I have the following:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
In my nginx configuration file I have the following:
location /static {
autoindex on;
alias /home/user/myproject/static/;
}
running ./manage.py collectstatic correctly places all my files into the static directory.
however, in running my server, all of these static files 404:
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/jquery/jquery.min.js HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 6418
The static files are placed in the same directory as my manage.py file. What am I doing wrong?
Adding this in my urls.py does work, but aren't I supposed to serve them directly from nginx instead?
if settings.DEBUG is False: #if DEBUG is True it will be served automatically
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
It's slower since static files rendering goes through Django instead
served by your web server directly

You should configure your nginx: edit nginx.conf the following way: add this:
location ~* ^(/static/|/media/).+.(jpg|jpeg|gif|png|zip|eot|woff|woff2|svg|ttf|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
root /path/to/parent/folder/of/static/folder;
}
to the server { clause corresponding to your website, then restart nginx and it should fix your problem, if not - check nginx logs, make sure that the folder has appropriate permissions for nginx user/group etc

Related

Static file issue when deploying an Django app

I understand there are some existing problems/answers about loading static files in production.
However, my case is somehow different. My Django app not is deployed as the main app of my website (not in the public_html folder in the server), but an additional app (in a folder outside public_html). The server folder directory is like this
home/myaccount/
├── public_html
| └── static (not exist, location 1)
├── mydjango
└── static (generated by collectstatic, location 2)
└── css, fonts etc.
In my settings.py, STATIC_ROOT = os.path.join(BASE_DIR, 'static/') and STATIC_URL = '/static/'.
Currently, the browser will look for static files in the non-existing location 1, i.e., www.mypage.com/static/..., instead of location 2. 404 errors return.
My question is that how I can change STATIC_ROOT and STATIC_URL or other settings to make the browser look for location 2 instead of location 1?
If you are using Linux server then an easier solution is to create a symbolic link of location 2 static folder in location 1.
ln -s /home/myaccount/mydjango/static /home/myaccount/public_html/static
Another solution can be to create STATIC_ROOT path to point to public_html folder.

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()

More Django static 404 issues - paired with ember

I've seen lots of different posts regarding the Django static file issues. Unfortunately, none of them seem to help me. My django admin css is fine, however static files are giving my a 404 error. Here is a description of what my problem:
In settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = '/Users/me/develop/ember/myproj/static/'
In urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My project directory:
> static
> myproj
> app
> templates
> ember.hbs
> img
> test.jpg
Inside my ember.hbs I reference the test.jpg, but I get a 404 error. It's weird because I can actually pull up the image using:
file:///Users/me/develop/ember/proj/static/myproj/img/test.jpg
I have no idea what to do to fix this, I've tried using the STATICFILES_DIRS method mentioned here: https://docs.djangoproject.com/en/1.8/howto/static-files/
but nothing seems to be working.
Thanks in advance!
More information: Django 1.8 and Ember-CLI.
I'm hosting the ember project within the static dir.
I'm running python manage.py runserver and running django on port 8000 at the same time as running ember s and running the application on port 4200. I'm using CORS-headers (https://github.com/ottoyiu/django-cors-headers/) to allow cross-site calls on development.
Static files will be discovered in "apps" that you create (found in INSTALLED_APPS) or in additional directories that you specify in STATICFILES_DIRS.
Let's say I did:
django-admin.py startproject myproject
And then:
cd myproject
./manage.py startapp myapp
mkdir myapp/static
mkdir myproject/static
Would give you a directory structure that looks something like
/Users/me/develop/myproject/
> myapp/
> migrations/
> static/
> __init__.py
> admin.py
> models.py
> tests.py
> views.py
> myproject/
> static/
> __init__.py
> settings.py
> urls.py
> wsgi.py
> manage.py
And then in settings.py you add "myapp" to INSTALLED_APPS.
In this structure, myapp/static/ files will be automatically discovered by Django because it is an installed app with a static directory. However, myproject/static/ files will NOT be discovered automatically because "myproject" is not in INSTALLED_APPS (and it shouldn't be).
This is because of the default STATICFILES_FINDERS setting, which is:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
]
The first finder, AppDirectoriesFinder, scans the directories of all of your INSTALLED_APPS, and discovers any "static" folders they might contain.
The second finder, FileSystemFinder, scans extra directories that you specify in STATICFILES_DIRS, which is an empty list by default.
So if you want those files in /Users/me/develop/myproject/myproject/static/ to be discovered (as you probably do), you can add this to your settings.py:
from os import pardir
from os.path import abspath, dirname, join
# Get the root directory of our project
BASE_DIR = abspath(join(dirname(__file__), pardir))
# Add our project static folder for discovery
STATICFILES_DIRS = (
# /Users/me/develop/myproject/myproject/static/
join(BASE_DIR, 'myproject', 'static'),
)
Now, all of this is separate from STATIC_ROOT, which is a place where all static files are copied when you run ./manage.py collectstatic. For projects just starting out, this is usually in the project root folder: /Users/develop/me/myproject/static/
So we would add, in settings.py:
# /Users/me/develop/myproject/static/
STATIC_ROOT = join(BASE_DIR, 'static')
But this is only really used in production—it's a way to compile all of your static assets into a single spot, and then point /static/ at it with your server (e.g. NGINX).
During development, you can skip collectstatic altogether, and have Django discover and serve your static assets on the fly. Django provides a "serve" view to do this in django.contrib.staticfiles.views.serve.
Try this in your urls.py:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.views import serve
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', serve),
]
In your example, you are asking Django to serve files from STATIC_ROOT, which is also fine, but requires you to run ./manage.py collectstatic first, and every time your static assets change—which can be a hassle.
Ember and Django really shouldn't live in the same project. They are completely different frameworks with different tooling.
Think of your Django project as the backend application that serves an API. Think of your Ember project as one of possibly many clients that will consume your API. Others might include mobile applications, partner services, and so on.
When I start a new project that is going to use Django and Ember, I start a Django project called "myproject":
django-admin.py startproject myproject
And separately, an Ember project called "myproject-web":
ember new myproject-web
In development, I'll have two tabs open in Terminal.
First tab:
cd myproject
./manage.py runserver
Second tab:
cd myproject-web
ember serve
In production, I'll have two different servers, one at app.myproject.com that serves the Django application, and another one at myproject.com that serves the Ember application.
Later on, I might start a Swift project in a repository called "myproject-ios", and a Java project called "myproject-android". You get the idea.
In order to serve the static files into your django project you just need to add these lines to your `settings.py
STATIC_URL = '/static/'
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
and then in the main stucture of your project dictory needs to be like
>Project_dir
>App_dir
>static_dir
>templates
>manage.py
place your css, js, and other static contents into your static_dir

Django 1.8 serving static files during deployment

My project runs fine on the local sever but during deployment on Heroku, I get the following error
Collectstatic configuration error. To debug, run: $ heroku run python manage.py collectstatic --noinput
I understand this is due to STATIC_ROOT settings in settings.py, but I can't seem to point to the location of the static files properly.
This is my folder structure.
C:/
/Users
/Mac
/denv
/musicpro
/musicpro
/featureapp
/static
/featureapp
/css
/img
/js
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'musicpro/featureapp/static/featureapp/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static")
)

Acces image in folder without django collectstatic

I have a django web application which create screenshot calling an external python script.
But I'm concerned because every time I run the script I make a ./manage collectstatic to see the screenshots on my application. Soon I could have lots of collectstatic running simultaneously and it seems bad.
How can I have a folder where I can put the screens and then acces it with django without having to load it as a static file?
My current project looks like :
mysite
├── static
│ └── screenshots
| └── *.png
└── crowlers
├── wrapper.py
└── screenshot_robot.py
/opt/scripts/my_script.sh # launch wrapper.py and collectstatic
wrapper.py create .pngs in static/screenshots for my ./manage collectstatic to get them.
EDIT based on first answer:
I finally created a media directory at the root of my django project
Add the following in settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
In urls.py (To make it work with DEBUG=True when in developement state):
from django.conf import settings
## debug stuff to serve static media
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
static files are for your project's assets - css, js, images etc -, IOW things that are part of the project itself and you want to keep in your git/mercurial/whatever scc. Uploaded / dynamic /generated contents are supposed to go to the medias folder (settings.MEDIA_ROOT).

Categories

Resources