On Tue, Aug 2, 2011 at 5:19 AM, veva...@yandex.ru <veva...@yandex.ru> wrote:
> I see I don't understand how can I use static files using the
> development server. I saw several answers to this question of other
> people but they are too breaf for me. I think I follow advices but
> without any result.
> Books on Django I have don't explain this problem. Be so kind to
> explain me.
> My local computer has Windows. The project folder is c:/djangomysites/
> testjquery, it contains subfolders static and templates, static
> contains subfolders images and js.
> Today I changed Django 1.2.3 to Django 1.3. May I consider images for
> simplicity. For the same reason I don't use "os" function here.
>
> settings.py contains:
> STATICFILES_DIRS = (
>    'c:/djangomysites/testjquery/static',
> )
> STATIC_ROOT=''
> STATIC_URL='/static/'
>
> urls.py contains:
> urlpatterns = patterns('',
>    (r'^', 'testjquery.views.first'),
> if settings.DEBUG:
>    urlpatterns += patterns('',
>    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
>        { 'document_root': 'c:/djangomysites/testjquery/static' })
>
> views.py contains:
> def first(request):
>    return render_to_response('index.html',
> context_instance=RequestContext(request))
>
> index.html contains:
> <img src="{{ STATIC_URL }}images/picture.png">
>
> As a result, I get <img src="/static/images/picture.png"> in the
> produced html-file. But the image file can't be loaded.
> Thank You very much!
>

The staticfiles app works in several steps.

First, you define the folders that staticfiles will collect files
from. These are the project static file folders listed in
settings.STATICFILES_DIRS, and the per-application static folder, eg
<appname>/static.

Next, you instruct django to collect the files into place, using the
collectstatic management command. This collects up all the files from
all the places configured in the previous step, and places them in
settings.STATIC_ROOT. This step isn't required in development, the
files do not need to be collected together, although by running this
step, you will see precisely what files are collected and where they
are put.

Next, you configure your webserver to serve the files. In apache, you
would set "Alias $STATIC_URL $STATIC_ROOT" (obviously, replacing with
the correct values, '$STATIC_URL' is meaningless to apache).
In development, you add this to your urlconf:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()

Finally, you update your templates to use STATIC_URL to refer to the files.

It is usually helpful to put prefixes on your static files, since each
folder is imported as it is found, and when a duplicate filename is
encountered, the first found one is used. Eg, with this folder
structure:
.
├── app1/
│   └── static/
│       └── base.css
└── app2/
    └── static/
        └── base.css

only app1/static/base.css will be collected, and will be made
available at {{ STATIC_URL }}/base.css. It is better to put files
inside a folder that will uniquely identify them, eg:
.
├── app1/
│   └── static/
│       └── app1/
│           └── base.css
└── app2/
    └── static/
        └── app2/
            └── base.css

so that you can refer to {{ STATIC_URL }}/app1/base.css or {{
STATIC_URL }}/app2/base.css.

So, what you are not doing, is not configuring the urlconf correctly.
Use the example from the docs, you should not be specifying the
document_root.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to