Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on what you've posted so far, it looks like you're following the docs for <a href="https://docs.djangoproject.com/en/dev/howto/static-files/#using-django-contrib-staticfiles" rel="noreferrer"><code>django.contrib.staticfiles</code></a>. I agree that <a href="https://docs.djangoproject.com/en/dev/howto/static-files/#using-django-contrib-staticfiles" rel="noreferrer">the docs</a> can be difficult to follow especially if one is new to django. </p> <p>I believe the confusion stems from the fact that <code>django.contrib.staticfiles</code> has <strong>two</strong> modes of operation:</p> <ol> <li>During the development phase where the <a href="https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-runserver" rel="noreferrer">development server</a> is used, it dynamically searches for static files in predefined directories and make it available on <code>STATIC_URL</code></li> <li>For deployment, it assists in collating static files to a single directory (defined using <code>STATIC_ROOT</code>) so that the static files can be hosted using a webserver suited for static files. This collation is done using <code>python ./manage.py collectstatic</code>.</li> </ol> <p>Here's a quick summary of how to get up and running. I haven't had a chance to try it out so there may be mistakes. Hopefully this will help you get started and at the very least help you understand the docs. When in doubt, do refer to the docs. </p> <h2>Hosting static files on the development server</h2> <ol> <li><p>Make sure you have <code>'django.contrib.staticfiles'</code> in <code>INSTALLED_APPS</code></p></li> <li><p>Specify <a href="https://docs.djangoproject.com/en/dev/ref/settings/#static-url" rel="noreferrer"><code>STATIC_URL</code></a>. This will be the path where your static files will be hosted on.</p> <pre><code>STATIC_URL = '/static/' </code></pre></li> <li><p>Make sure your files are in the correct directories. By default, <code>staticfiles</code> will look for files within the <code>static/</code> directory of each installed app, as well as in directories defined in <a href="https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-dirs" rel="noreferrer"><code>STATICFILES_DIRS</code></a>. (This behaviour depends on backends listed in <a href="https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders" rel="noreferrer"><code>STATICFILES_FINDERS</code></a>). In your case, you'd probably want to specify your directory in <code>STATICFILES_DIRS</code>:</p> <pre><code>STATICFILES_DIRS = ( 'C:/Users/Dan/seminarWebsite/static/', ) </code></pre></li> <li><p>Make the view accessible by adding the following to the <strong>end</strong> of <code>urls.py</code>:</p> <pre><code>from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns() </code></pre></li> <li><p>Make sure you have <code>DEBUG = True</code> in <code>settings.py</code>. </p></li> </ol> <p>That's it. </p> <p>If you run your dev server (<code>./manage.py runserver</code>), you should be able to access your file via <code>http://localhost:8000/static/images/vision.jpeg</code> (which serves <code>C:/Users/Dan/seminarWebsite/static/images/vision/jpeg</code>).</p> <h2>Linking to static files in your templates</h2> <p>There are two ways to get a correct link for your static files - using the <a href="https://docs.djangoproject.com/en/dev/howto/static-files/#with-a-template-tag" rel="noreferrer">staticfiles template tag</a>, and making <code>STATIC_URL</code> accessible to your templates. Since you've attempted the latter, we'll stick to that.</p> <ol> <li><p>Make sure you have <code>'django.core.context_processors.static'</code> in <code>TEMPLATE_CONTEXT_PROCESSORS</code>. If you haven't redefined <code>TEMPLATE_CONTEXT_PROCESSORS</code> then <strong>there is nothing to do</strong> since <a href="https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors" rel="noreferrer">it should be there by default</a>.</p></li> <li><p>Make sure you use <a href="https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext" rel="noreferrer">RequestContext</a> when rendering your template. Example:</p> <pre><code>from django.template import RequestContext # ... def some_view(request): # ... return render_to_response('my_template.html', { "foo" : "bar", # other context }, context_instance = RequestContext(request)) </code></pre></li> </ol> <p>You should now be able to use the following in your <code>my_template.html</code>:</p> <pre><code>&lt;a href="{{ STATIC_URL }}images/vision.jpeg" /&gt; </code></pre> <h2>Hosting static files on production server.</h2> <p>If all the static files you need to serve are store in that one directory (<code>C:/Users/Dan/seminarWebsite/static</code>), then you're almost there. Simple configure your webserver to host that directory on <code>/static/</code> (or whatever you set <code>STATIC_URL</code> to) and you're good to go.</p> <p>If you have files scattered in different directories and/or app specific static files, then you'll need to collate them.</p> <ol> <li><p>Set <a href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_ROOT" rel="noreferrer"><code>STATIC_ROOT</code></a> to the directory where you want to store the collated files.</p></li> <li><p>Run <a href="https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic" rel="noreferrer"><code>./manage.py collectstatic</code></a> to do the collation.</p></li> <li><p>Configure your webserver to host the that directory (<code>STATIC_ROOT</code>) on <code>/static/</code> (or whatever you set <code>STATIC_URL</code> to).</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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