robertsan
robertsan

Reputation: 1621

Where do you keep your media folder in a Django project?

I'm wondering where do you Django developers keep your media folder. I'm currently developing a project located at ~/apps/django-project. I am not using continuous delivery at the moment, but when I have to update the project, I ssh straight into the remote machine and pull the updated files using git (that's because it is placed in the project folder). The problem is that the media folder is always updating and I'll have to track them using git, which is not something I want to do. I don't think I'd like to .gitignore them too.

Would it be a good idea to structure the project like that?

Project: ~/apps/django-project

Media: ~/media

Statics: ~/static

If that's a good idea, could you give me a hint about how to set up my settings.py MEDIA_ROOT and STATIC_ROOT in order to accomplish this?

If that's not a good idea, I'd like to hear from you how would one structure the project and what other good principles should I take into account. (also, development / production tips are welcome)

Django3 with Python3.7

Upvotes: 1

Views: 989

Answers (1)

Mario César
Mario César

Reputation: 3737

I create a folder public in my root directory and there I add the media and static directories

public/
   media/
   static/

I also add the specific paths to .gitignore, so they don't conflict between environments.

public/media
public/static

The good thing about this approach is that if you are using a webserver like nginx or uwsgi you can set the document root to public and serve any static file by default and resolve to django any other path that is not a file in public.

For example in nginx I do something like this.

server {
    root /var/sites/myproject/public;

    location @djangoapp {
        proxy_redirect off;
        proxy_pass http://localhost:8000;
    }

    location / {
        try_files $uri @djangoapp; 
    }
}

This is very convenient because is easy to reason about public, everything that is in that folder will be served statically. For example I put there my robots.txt file and some others that I know they just need to be simple serve. It defaults nicely and really fast to django for any other request that is not an static file in public

public/
   media/
     attachs/
     users/
   static/
     admin/
     css/
     js/
   robots.txt
   humans.txt
   manifest.json
   favicon.ico

I even once added support for .php files where I just put the files there and add the .php setup as an extension rule in nginx.

Upvotes: 2

Related Questions