serving static files on Django production tutorial
Asked Answered
G

4

15

Does anyone have a simple step-by-step tutorial about serving static files on a Django production app? I read the Django docs and it sounds really complicated... I'm trying to go the route of serving static files using a different server like lighttpd, nginx, or cherokee, but setting these up is all Greek to me. I downloaded lighttpd, tried to follow the instructions to install, and within a few seconds got an error. Missing this or that or whatnot... I'm not a UNIX whiz and I'm not very good at C/C++, so all this ./configure and MAKE install are gibberish to me... So I guess my immediate questions are:

  1. Which server would you recommend to serve static files that's easy to install and easy to maintain?
  2. Assuming I actually get the server up and running, then what? How do I tell Django to look for the files on that other server?
  3. Again, anyone has step-by-step tutorials?

Thanks a lot!

Groping answered 22/4, 2011 at 14:49 Comment(7)
Are you looking for this? hhttp://docs.djangoproject.com/en/1.3/howto/static-files/ It seems to be step-by-step.Smew
Yeah I've read that doc. I didn't find it detailed enough. For example, is there an answer my #2 question in there? I might have missed it.... Thanks!Groping
@rabbid: After re-reading your question (which is rather confusing) I think that is the wrong link.Smew
Sorry for the confusion!Groping
@rabbid: Don't apologize. Fix the question. Simplify. Focus. Clarify.Smew
@rabbid: I can only recommend cherokee webserver for django applications. It's dramatically more performant than apache (with mod_python) and easily configurable through its web interface. But: I'm sorry to tell you that when you want to install a cherokee webserver with django support (through uWSGI) you most probably gonna have to compile one or two modules by yourself. Some basic linux knowledge is required for that.Elfredaelfrida
@rabbid: Which OS does your production server run?Elfredaelfrida
B
6

Sorry, don't have a step by step tutorial. But here is a high level overview that might help:

  1. You probably want to go with the Apache server ( http://httpd.apache.org/) This comes with most *nix distributions.
  2. You then want to use mod python (or as the commenter pointed out mod_wsgi: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/) to connect to Django : http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs. Once you complete this step, Apache is now fronting for Django.
  3. Next you want to collect the static files in your Django into one directory and point apache at that directory. You can do this using the the ./manage.py collectstatic if you used django.contrib.staticfiles (http://docs.djangoproject.com/en/dev/howto/static-files/.)

So the trick is you're not telling Django to delegate serving static files to a specific server. Rather you're telling httpd which urls are served via Django and what urls are static files.

Another way of saying this is that all requests come to the Apache web server. The webserver, according to the rules you specify in httpd.conf, will decide whether the request is for a static file or whether it is for a dynamic file generated by django. If it for a static file it will simply serve the file. If the request is for a dynamic file it will, via modpython, pass the request to Django.

Hope that helps.

Bagby answered 22/4, 2011 at 15:2 Comment(5)
wsgi or uwsgi is definitely the preferred way to go nowadays. mod_python is slow and won't be supported soon. docs.djangoproject.com/en/dev/howto/deployment/modwsgiShudder
Thanks for your replies. Yeah I get what you're saying. I've read that much from the official Django docs. It looks like you're describing how to serve the app itself through Apache. Are you saying to serve static files through Apache as well? I think the doc says that is not a recommended route. You think it's ok? The route I chose was to serve static files through a different server like lighttpd or cherokee. However, I'm not comfortable at all MAKEing and installing from source, and manually configuring everything. I don't know how lighttpd/cherokee would talk with my Django app either..Groping
@rabbid: It's fine to serve the static media from Apache -- what you don't want to do serve your app and its media using Django's development server.Carrew
@mipadi: yeah of course, I wouldn't use the dev server on production. I guess I'll try serving static files from the same Apache server first. Thanks!Groping
@rabbid: I think the docs actually tell you to use Apache to server static files. They tell you not to use the development server in production, or to server static files through Django.Bagby
E
5

With the latest Django version like Django 3.2.6 I was having issues serving media and static files both in the dev and prod environment while DEBUG = False.

So I got around a solution that came from multiple stack overflow posts.

  1. Import appropriate functions to urls.py
from django.urls import include, path, re_path
from django.views.static import serve
  1. Define static URL pattern list to urls.py
static_urlpatterns = [
    re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT}),
    re_path(r"^static/(?P<path>.*)$", serve, {"document_root": settings.STATIC_ROOT}),
]

Assuming your STATIC_ROOT and MEDIA_ROOT is already defined in settings.py file

  1. Just include static_urlpatterns in urlpatterns
urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/", include(api_urlpatterns)),
    path("", include(static_urlpatterns)),
]

Hope it works for you both in the dev and prod environment when DEBUG = FALSE. Thank you.

Equivalency answered 9/12, 2021 at 17:1 Comment(2)
Thanks for sharing ! quick question, where does 1 and 2 go? urls.py? or settings.py?Scopula
1 and 2 both should go to urls.py. Thanks for the mention @ScopulaEquivalency
A
3

Development

STATICFILES_DIRS should have all static directories inside which all static files are resident.

STATIC_URL should be /static/ if your files are in local machine otherwise put the base URL here e.g. http://example.com/.

INSTALLED_APPS should include django.contrib.staticfiles.

In the template, load the staticfiles module:

{% load staticfiles %}
<img src='{% static "images/test.png" %}' alt='img' />

Production

Add STATIC_ROOT that is used by Django to collect all static files from STATICFILES_DIRS to it.

Collect static files:

$ python manage.py collectstatic

Add the path to urls.py:

from . import settings

urlpatterns = patterns('',
..
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)

More detailed articles are listed below:

Anabolite answered 16/3, 2015 at 22:8 Comment(0)
R
1

Updated for urls.py

the url(....) format doesn't work anymore in urls.py for Django 3.0.7.

you need to do then:

urls.py:

from django.conf import settings # to import static in deployment
from django.conf.urls.static import static # to import static in deployment
....
urlpatterns = [
....

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # to import static in deployment

Reference: https://docs.djangoproject.com/en/3.0/howto/static-files/

Reseta answered 21/6, 2020 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.