Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Folder Setup:</strong> </p> <p>Your project root should be something like:</p> <pre><code>/app1 /app2 /media /static /templates urls.py settings.py manage.py </code></pre> <p>The <strong>media</strong> folder is supposed to hold things like images, downloads and other material that might be uploaded during normal use of the website (i.e. after development is finished)</p> <p>The <strong>static</strong> folder is supposed to hold all the CSS/JS and other material that is a part of the development of the site</p> <hr> <p><strong>Settings.py:</strong></p> <p><strong>MEDIA_ROOT</strong> is the absolute server path to the static folder mentioned above. That means it should be something like:</p> <pre><code>MEDIA_ROOT = "/User/Bob/Sites/MySite/Project_root/media/" </code></pre> <p><strong>MEDIA_URL</strong> is the relative browser URL you should access your media files from when you are looking at the site. It should be (usually)</p> <pre><code>MEDIA_URL = "media/" </code></pre> <p>which means all material can be viewed at <a href="http://example.com/media/" rel="noreferrer">http://example.com/media/</a></p> <p>Similarly, <strong>STATIC_ROOT</strong> should be something like</p> <pre><code>STATIC_ROOT = "/User/Bob/Sites/MySite/Project_root/static/" </code></pre> <p>and <strong>STATIC_URL</strong> be</p> <pre><code>STATIC_URL = "static/" </code></pre> <hr> <p><strong>Serving the files:</strong></p> <p>Now that you have told django where these folders should be, and the correct URLs to access them, you need to serve all requests to the folders correctly. </p> <p>Usually when you are in production, you want the webserver to take care of serving your static files and media files.</p> <p>If you are developing though, you can just get the django development server to serve them for you. </p> <p>To do this, you tell it to route all request that come in to <a href="http://example.com/media" rel="noreferrer">http://example.com/media</a> to your MEDIA_ROOT and all requests that come in to <a href="http://example.com/static" rel="noreferrer">http://example.com/static</a> to your STATIC_ROOT. </p> <p>To do this, you add some URLS to URLS.py like you have:</p> <pre><code>from django.conf import settings if settings.DEBUG: urlpatterns += patterns('', url(r'^media/(?P&lt;path&gt;.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), url(r'^static/(?P&lt;path&gt;.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT, }), ) </code></pre> <hr> <p><strong>Extra</strong>:</p> <p>If you have multiple apps, each with their own CSS and JS files, you mightn't want to throw them into one single /static/ folder. It might be useful to put them in subfolders of the apps they belong to:</p> <pre><code>/app1/static/ # Specific static folder /app2/static/ /media/ /static/ # Root static folder </code></pre> <p>Now, your webserver/development server is only looking for static files where you told it to look (i.e. the root static folder) so you need to collect all the files in the subfolders and copy them to the root static folder. You could do this by hand, but django provides a command to do this for you (this is the whole point of the static app)</p> <pre><code>./manage collectstatic </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload