static file are not working in django nginx - python

I have created one small app, and uploaded to the aws server, i can see everything is working except static folder, my static folder is under the /home/ubuntu/django-bhuriyo/mysite/ this directory, i am using nginx, i have put my code of nginx conf and settings.py, can anyone please look my code and help me to resolve this issue ?
django.conf
server {
listen 80;
server_name ****.amazonaws.com;
location /static {
alias /home/ubuntu/django-bhuriyo/mysite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/django-bhuriyo/app.sock;
}
}
settings.py
STATIC_URL = 'static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = '/home/ubuntu/django-bhuriyo/mysite'

I see everything fine... are you collecting the files?
python manage.py collectstatic
You need that command for Django to copy over all your static files to the directory specified in the STATIC_ROOT setting. Remember you have to execute that every time you have some change in your static files.

You also have to declare the path of the media.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and location for nginx :
location = /media/ {
root /path/to/medias;
}
what is your error traceback ?

Related

Django development server command

I am a little confused about the Django development server. Here the question is if my project running based on gunicorn and Nginx in production environment.
Should my local development need Nginx for serving static files?
if yes then what command should I use instead of Python manage.py runserver.
Help me get out of it.
For static file, you have to manage below variables in your settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'your_app_name/static')
]
For media related thing you have to set below variables in your settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Django static files : use only /static/ directory

I have a Django live version in production and I develop in local with Django manager.py.
I'm not able to find the correct configuration settings for my static files.
All my static files are on a /static/ on the root of the project, because all the sub-apps use the same stylesheets and so on.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
On my live version : it works because I've got an NGINX rule that allow access to /static/ directly.
But when I want to work on local, the static files are 404, with Debug True and False.
I don't see the interest for my needings of the staticfiles directory (but I can have a wrong mind here).
Is it possible to have all the static files in subdirectories in /static/ (css, img, js, etc.) and to set a workable settings on both local and live production without switching something in settings.py for deployment ?
Thanks for your help :)
I've found the solution :
After moving my static directory from the root directory of Django the folder of my main app, I made a collectstatic with that settings :
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

Django - Static files don't serve in production

My site works well in local but when I put it on my server, it can't load the static files. Here's my files.
settings.py
DEBUG=False
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
All my static are in /myproject/mymainapp/static
I've got another directory "static" of the root of my project by doing manage collectstatic in production.
Here's my nginx conf file :
[...]
root /home/user/site/mymainapp/;
[...]
location /static {
alias /home/user/site/mymainapp/static;
}
I've tried to set the path to my static directory in my root directory project but it don't make anything better.
My URLs patterns :
urlpatterns = [
[...] nothing relative to static files
]
Maybe I should add something in my URLs patterns ?
Thanks for your help :)

serving static vs media in django 1.9

To serve the static files (CSS, JS), I set the settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
This means that all my static files need to be in the project root's static folder
/app1
/app2
/media
/static #They need to be stored here, all static files
/templates
urls.py
settings.py
manage.py
However, I am storing them in my app's static folder.
/app1/static/ # Storing the static files here
/app2/static/ #and here
/media/
/static/ # but not here
Still, Django is able to serve them, how is that possible?
I tried the same thing with media files; setting
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and storing the media (images etc) files in each app's individual media dir. This time, Django did not serve the files and served them only when either I moved the files to the project root's media dir or changed the setting to MEDIA_ROOT = os.path.join(BASE_DIR, '<app_name>/media')
Why was I allowed to serve static files even when they weren't in the project root static dir but not the media files - they were only served from the root media dir.
Please read the documentation carefully - https://docs.djangoproject.com/en/1.9/ref/settings/#staticfiles-finders
It precisely states that django will search for static files in each app + the directory stated in settings.
Quoting as in the documentation -
The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.
While this is not true for media files. Django doesn't look for media files in subdirectories.

Django with ./manager runserver works well, while Django + uwsgi return 404

This si my settings.py about static in settings.py:
STATIC_ROOT = '/home/coat/www/site/app/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/usr/lib/python2.6/site-packages/django/contrib/admin/static/",
# This is Django admin default static files
)
I user django server:
./manager runserver
Then I open the URL: http://localhost:8000/static/admin/css/base.css
It works very well.
But a open http://localhost/static/admin/css/base.css
It print '404'
I had restart Nginx and uwsgi for many times, but it dosen't works.
First things first, this is nonono:
STATIC_ROOT = '/home/coat/www/site/app/static/'
Never hardcode absolute paths, you're just making your settings file less portable and probably killing kittens. Adapt this to your needs:
import os.path
import posixpath
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# fix STATICFILES_DIRS too
Now, to your question. django.contrib.staticfiles is fantastic but probably a little confusing at first.
You must understand the collectstatic command:
Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
With runserver, staticfiles are served automatically, but in production mode (DEBUG=False, real HTTP server like Nginx), you should run collectstatic to (re)build STATIC_ROOT
STATIC_ROOT: is the root path where the HTTP server should serve static files from.
STATIC_URL: is the root URL where the HTTP server should serve static files to.
STATICFILES_DIRS: other static directories, in addition to each app's "static" subdirectory. Because django.contrib.admin is a normal app with a "static" folder, there is no need to specify it in the settings.
Conclusion: if STATIC_ROOT resolves to /home/coat/www/site/app/static/, and STATIC_URL is /static/, then you should:
Run collectstatic management command
Configure Nginx to serve /home/coat/www/site/app/static/ on /static/, ie.:
location ^~ /static/ {
alias /home/coat/www/site/app/static/;
}
Reload nginx
In development the runserver command does some magic to serve your static files. In production you need to configure NGinx to do it. There are two chapters in the Django documentation I recommend you read:
Serving static files in production
Serving files (Deployment)
Especially the last link explains how you have to configure your NGinx to serve your static files.

Categories

Resources