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()
Related
I have a Django server running with nginx and gunicorn.
All static files are served correctly, only the admin page has no css, js, etc. .
I already ran collectstatic and restarted nginx and gunicorn.
Nothing changed.
What can I do?
Make sure to mention the locations of your staticfiles_dirs in settings.py. Also remember to mention the locations of your static files in a dynamic format instead of hard coding them in your html document.And make sure to load static in base.html.
Try using this sample code in your settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'templates'),
'/var/www/static/'
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
After this use the following command in terminal:
python manage.py collectstatic
This should create the static folder and copy all the static objects inside
I am using Django 2.2. From the Django Managing static files documentation:
If you use django.contrib.staticfiles as explained above, runserver
will do this automatically when DEBUG is set to True. If you don’t
have django.contrib.staticfiles in INSTALLED_APPS, you can still
manually serve static files using the django.views.static.serve()
view.
This is not suitable for production use! For some common deployment
strategies, see Deploying static files.
For example, if your STATIC_URL is defined as /static/, you can do
this by adding the following snippet to your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Note
This helper function works only in debug mode and only if the given
prefix is local (e.g. /static/) and not a URL (e.g.
http://static.example.com/).
Also this helper function only serves the actual STATIC_ROOT folder;
it doesn’t perform static files discovery like
django.contrib.staticfiles.
My Interpretation
static is a helper function that serves files from the STATIC_ROOT during development (is this True?)
static only works when debug = True
static only works with a local prefix like STATIC_URL = '/static/'
When DEBUG is set to True and I use and setup the staticfiles app as explained in the documentation, if I do python manage.py runserver to start the local server, the serving of static files will be handled automatically (true??)
My Questions
What EXACTLY does adding static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to your project's urls.py DO?
Is it true that static serves static files locally from the STATIC_ROOT directory? To test this theory, after running collectstatic, I then deleted the static directories to see if the static files still load fine (from the STATIC_ROOT) and they DON'T! Why?
How can I verify that Django is loading the static files from my STATIC_ROOT location... and not the static directories in my project and apps??
Why is adding static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to urlpatterns necessary if Django serves static files automatically (mentioned in documentation)?
Example
settings.py
DEBUG = True
...
INSTALLED_APPS = [
'django.contrib.admin',
...
'django.contrib.staticfiles',
'puppies.apps.PuppiesConfig'
]
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = 'c:/lkdsjfkljsd_cdn'
In all my templates, I'm using {% load static %}.
Then I do: python manage.py collectstatic
At this point, it doesn't seem to matter if I have the below in my urls.py or not - my static files still load BUT I don't know if they're coming from my project's static directories or my STATIC_ROOT (c:/lkdsjfkljsd_cdn):
if settings.DEBUG is True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Lastly, if I delete those static directories in my project, all css, js and images don't work which leads me to believe that my Django project is loading static files from my project's static directories, NOT the STATIC_ROOT.
So, again, how can I tell Django to load the static files from my STATIC_ROOT location... and not the static directories in my project and apps?? OR, do I misunderstand that Django isn't supposed to load files from my STATIC_ROOT location locally?
*Edit (adding HTML image)
I think you are mixing up few things. Let me clarify.
static:
As per documentation, it provides URL pattern for serving static file. If you go to the implementation, then you will see:
return [
re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
]
What it does is, it removes forward slash from left of the prefix(ie converts /static/ to static/), and then there is a view(which is serve) who does the pulling files.
serve:
This function does the serving files. It will serve files from a document root.
runserver:
runserver command runs the django development server. If you have django.contrib.staticfiles installed in INSTALLED_APPS, then it will automatically serve static files. If you don't want to serve static, then use runserver --nostatic. collectstatic or STATIC_ROOT has no relation to this command.
collectstatic:
This command collects all static files from different STATIC_DIRS, and put them in folder which is defined by STATIC_ROOT. collectstatic is very useful in production deployment, when you are using a reverse proxy server like NGINX or Apache etc. NGINX/Apache/Varnish uses that folder (where collectstatic stored the static files) as root and serve static from it. It is not recommended to use runserver in production. You can use gunicorn/uwsgi to serve django. But gunicorn/uwsgi does not serve static files, hence using reverse proxy server to serve static files.
finally:
To answer your questions:
No you don't have to put that in your code, unless you are not adding django.contrib.staticfiles in your INSTALLED_APPS.
No
You don't need to. STATIC_ROOT is used for different purpose
It is not. But for serving MEDIA files, you can add the following pattern:
if settings.DEBUG:
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
In production, media files should be served by reverse proxy server as well.
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
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.
When developing a Django application in debug mode, I serve static files using the following code:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^m/(?P<path>.*)$', serve, {
'document_root' : os.path.join(os.path.dirname(__file__), "media")
})
)
I am using nginx as a front end to server my static files in production mode using the following nginx config:
location m/
{
root /path/to/folder/media/;
}
Which seems sub-optimal because I have to create a "m" folder in the media directory. I am wondering what everyone else's Django/nginx config files look like. Specifically, can you please cut-and-paste sections of nginx.confg and urls.py (settings.DEBUG == True)
Thanks.
Using Django 1.3 django.contrib.staticfiles will take care of serving everything for you during development. You don't need to do anything particular in the urls.py. I wrote a little guide for myself after the Django 1.3 update that covers the settings to use:
# idiom to get path of project
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
# url prefix for user uploaded files, stuff that django has to serve directly
MEDIA_URL = '/media/'
# url prefix for static files like css, js, images
STATIC_URL = '/static/'
# url prefix for *static* /admin media
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
# path to django-served media
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
# path used for collectstatic, *where the webserver not django will expect to find files*
STATIC_ROOT = '/home/user/public_html/static'
# path to directories containing static files for django project, apps, etc, css/js
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static'),
)
# List of finder classes that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
# Required for all the magic
INSTALLED_APPS = (
'django.contrib.staticfiles',
)
Refer to the docs for details: http://docs.djangoproject.com/en/1.3/howto/static-files/.
I use nginx and uwsgi for serving django apps in production (I use runserver for development). I symlink my /static and /media folders (from my django project) into /var/www/vhosts/domain.com/html for nginx to find. You could also use the collectstatic command instead of symlinking. If it can't find a static file it falls back to uwsgi (which is running the django app).
Instead of uwsgi you could use fast-cgi, or proxy_pass or whatever you want. I prefer uwsgi because it has an incredible number of features and great performance. I run uwsgi as a daemon with: uwsgi --emperor '/srv/*/*.ini'. This is a fairly new option, it tells uwsgi to scan a given path for configuration files. When the emperor uwsgi daemon finds a configuration file it launches a new instance of uwsgi using the configuration found. If you change your configuration the emperor uwsgi daemon will notice and restart your app for you. You can also touch the config file to reload like with mod_wsgi, and it's really easy to setup new apps once you have everything configured initially.
The path conventions I follow are:
/srv/venv/ - virtualenv for project
/srv/venv/app.ini - configuration for uwsgi
/srv/venv/app.sock - uwsgi sock for django
/srv/venv/app.wsgi - wsgi file for uwsgi
/srv/venv/proj - django project
/srv/venv/proj/settings.py - project settings file
/srv/venv/proj/static - static files dir, linked into var/www/vhosts/domain.com/html
/srv/venv/proj/static/admin - admin static files, linked as well
/srv/venv/proj/media - media files dir
/var/www/vhosts/domain.com/html - base directory for nginx to serve static resources from
This is my nginx.conf:
location / {
root /var/www/vhosts/domain.com/html;
index index.html index.html;
error_page 404 403 = #uwsgi;
log_not_found off;
}
location #uwsgi {
internal;
include /etc/nginx/uwsgi_params;
uwsgi_pass unix:/srv/venv/app.sock;
}
My uwsgi ini file (you can also use xml/yaml/etc):
[uwsgi]
home = /srv/%n
pp = /srv/%n
wsgi-file = /srv/%n/%n.wsgi
socket = /srv/%n/%n.sock
single-intepreter = true
master = true
processes = 2
logto = /srv/%n/%n.log
You should also check out gunicorn, it has really nice django integration and good performance.
I'm putting this here in case someone wants an example of how to do this for Apache and WSGI. The question title is worded such that it's not just covering nginx.
Apache/WSGI Daemon
In my deployment, I decided to keep the database connection info out of the settings.py file. Instead I have a path /etc/django which contains the files with the database configuration. This is covered in some detail in an answer to another question. However, as a side effect, I can check for the presence of these files and the project being in a certain path to determine if this is running as a deployment, and in settings.py I define the settings IS_DEV, IS_BETA, and IS_PROD as True or False. Finding the project's directory from settings.py is just:
# Find where we live.
import os
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
Anything that needs a path into the project uses BASE_DIR. So in urls.py, I have at the end:
# Only serve static media if in development (runserver) mode.
if settings.IS_DEV:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True}),
)
(I also have another URL in there that I use for UI testing and don't want on beta or production.)
This covers the development server case. For production, I just have to set the Apache configuration up to serve the static stuff. (This is an intranet application with low to medium load, so I don't have a lightweight webserver like lighttpd to serve the static stuff, contrary to the Django docs' recommendation.) Since I'm using Fedora Core, I add a django.conf file in /etc/httpd/conf.d that reads similar to:
WSGIDaemonProcess djangoproject threads=15
WSGISocketPrefix /var/run/wsgi/wsgi
Alias /django/static/ /var/www/djangoproject/static/
Alias /django/admin/media/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
WSGIScriptAlias /django /var/www/djangoproject/django.wsgi
WSGIProcessGroup djangoproject
<Directory /var/www/djangoproject>
Order deny,allow
Allow from all
</Directory>
<Directory /usr/lib/python2.6/site-packages/django/contrib/admin/media>
Order deny,allow
Allow from all
</Directory>
IIRC, the key here is to put your Alias lines before your WSGIScriptAlias line. Also make sure that there's no way for the user to download your code; I did that by putting the static stuff in a static directory that is not in my Django project. That's why BASE_DIR gives the directory containing the Django project directory.
You can omit the WSGISocketPrefix line. I have it because the admin wants the sockets in a non-default location.
My WSGI file is at /var/www/djangoproject/django.wsgi (i.e. /django.wsgi in the Mercurial repository) and contains something like:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproject.settings'
os.environ['DB_CONFIG'] = '/etc/django/db_regular.py'
thisDir = os.path.dirname(__file__)
sys.path.append(thisDir)
sys.path.append(os.path.join(thisDir, 'djangoproject'))
sys.path.append(os.path.join(thisDir, 'lib'))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The nice thing about WSGI daemons is that you just have to touch django.wsgi to restart your Django WSGI daemon; you don't need to reload or restart the Apache server. This makes the admin happy.
Finally, since my /var/www/djangoproject is just a Mercurial repo, I have the following in /var/www/djangoproject/.hg/hgrc:
[hooks]
changegroup.1=find . -name \*.py[co] -exec rm -f {} \;
changegroup.2=hg update
changegroup.3=chgrp -Rf djangoproject . || true
changegroup.4=chmod -Rf g+w,o-rwx . || true
changegroup.5=find . -type d -exec chmod -f g+xs {} \;
changegroup.6=touch django.wsgi # Reloads the app
This clears Python bytecode, updates the working copy, fixes up all the perms, and restarts the daemon whenever a developer pushes into deployment, so anyone in the djangoproject group can push into it and not just the last one who added a file. Needless to say, be careful who you put in the djangoproject group.
zeekay sets STATIC_URL, STATIC_ROOT, STATICFILES_DIRS, STATICFILES_FINDERS and uses "django.contrib.staticfiles" and "django.core.context_processors.static" in his settings. I don't have those since my code dates back to Django 1.1 and don't use {{ STATIC_ROOT }}.
Hope this helps.