Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Where to store global static files and templates?

Tags:

So I am working on a Django (1.9) project, and have the need to use the same templates and static files across multiple apps. I know how to use templates and static files stored outside each app (using STATICFILES_DIRS and TEMPLATEFILES_DIRS in settings.py), but where should I store them?

My guess would be in folders in the project directory.

home/user/myproject/staticfiles home/user/myproject/templates 

But if there are official reconsiderations (which I have been unable to find in the documentation), I would prefer to comply with those.

Also, if there are any other recommendations for using templates and static files outside of the normal django app directories, that you think I should know about, I'd really appreciate you telling me (or telling me where to look).

Cheers :)

like image 362
99lives Avatar asked Mar 06 '16 09:03

99lives


People also ask

Where should I store static files?

Apps in the standard environment can serve static files from a Google Cloud option like Cloud Storage, serve them directly, or use a third-party content delivery network (CDN). Hosting your static site on Google Cloud can cost less than using a traditional hosting provider, as Google Cloud provides a free tier.

How do you serve static files in Django in production?

Serving static files in production. The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory ( STATIC_ROOT ) to be moved to the static file server and served.

What is the difference between media and static files in Django?

Static files are meant for javascript/images etc, but media files are for user-uploaded content.


1 Answers

Assume there is a global css file located here: home/user/myproject/staticfiles/css/global.css

Change settings.py to match this:

STATIC_URL = '/static/' STATICFILES_DIRS = [     os.path.join(BASE_DIR, "staticfiles"),  ] 

Inside of a template, for example, index.html:

{% load static %}  <link rel="stylesheet" type="text/css" href="{% static 'css/global.css' %}" />    

Now Django can find global.css.

like image 184
nu everest Avatar answered Sep 27 '22 22:09

nu everest