Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4577610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:22:11+00:00 2026-05-21T20:22:11+00:00

When developing a Django application in debug mode, I serve static files using the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:22:12+00:00Added an answer on May 21, 2026 at 8:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a Django app, and I'm trying to use Python's logging module for
I'm developing a website (in Django) that uses OpenID to authenticate users. As I'm
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a .NET WinForms application: how can I check if the window is in
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
Developing server side code i finally got my eyes X-crossed trying to write -
When developing a desktop application in .NET, is it possible to not require the
When developing a new web based application which version of html should you aim
Developing a website and just trying to get back into the swing of (clever)
When developing whether its Web or Desktop at which point should a developer switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.