PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

How to Deploy Django Static Files in Nginx!

How to Deploy Django Static Files in Nginx!

This article shows how to deploy Django static files in Linux. If you want to deploy in Google Cloud, create your account by using my referral link here. You can get free $350 credit and also help me!


Settings.py

In your project_name/settings.py, specify the your app static file directories in STATICFILES_DIRS list. In STATIC_ROOT variable, specify the target directory for your static files. You can choose any directory that is not linked to a user. It should be visible to:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "<app_name>/static"),
]

STATIC_URL = "/static/"
STATIC_ROOT = "/var/www/<project_name>/static/"

Collect static files

Django does not automatically copy all your static files to STATIC_ROOT. You can do this manually by running:

python manage.py collectstatic

Nginx configuration

In your nginx configuration add a location section.

server {
    ...

    location /static/ {
        root /var/www/<project_name>/;
        autoindex off;
    }
   
    ...
}

If you found this usful then please share this and follow me! Also check out my website where I also post everything from here

Comments (0)

loading comments