Can't set static file directory.

157 views
Skip to first unread message

Mārtiņš Jakubovičs

unread,
Jan 5, 2013, 1:38:32 PM1/5/13
to django...@googlegroups.com
Hello.

I try a lot of things and can't understand, why not working STATIC_ROOT and MEDIA_ROOT in settings.py.

I want, that all my media and static folders is in different place that python files, so I set media, static and templates to different place. Templates TEMPLATE_DIRS works well, bet MEDIA_ROOT and STATIC_ROOT not.

I try setup like this:

/home/domain/www/my_proj/ there is project folder and all apps, and there I place my htdocs folder, in which is static, templates and media folders.

When i set in STATIC_ROOT = '/home/domain/www/my_proj/htdocs/static'

In apache error log i got:

File does not exist: /home/domain/www/my_proj/my_proj/static

I don't get, why django don't want to take new setting...

I use django 1.4.

Thanks.

Pedro J. Aramburu

unread,
Jan 6, 2013, 9:28:58 AM1/6/13
to django...@googlegroups.com
Because it's configured that way in apache, not in django, and it doesn't exist ergo the error message.

Pedro J. Aramburu

unread,
Jan 6, 2013, 9:31:20 AM1/6/13
to django...@googlegroups.com
That is in a development server? Why not use ./manage.py runserver? If not you can point apache to the wsgi file and use django itself to serve the static files modifying a little bit urls.py. But it's not recommended for production use.


The built in dev server does it by default (so no need to modify urls.py).


El sábado, 5 de enero de 2013 15:38:32 UTC-3, Mārtiņš Jakubovičs escribió:

joy

unread,
Jan 8, 2013, 6:03:48 AM1/8/13
to django...@googlegroups.com
I have the same problem, the tutorial speaks about a list of things to do:

I have arrived at the second point. But the static files are not served, i have set up the STATIC ROOT and the STATIC_URL, but what should i add to the template src tag to find the pages? The tutorial speaks about the template context processor, but it doesn't exist in my setting.py file.

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
)

i cannot make my css and javascript work, please, can you explain me what is happening behind the interface?
Agnese

Mike Dewhirst

unread,
Jan 8, 2013, 6:20:54 AM1/8/13
to django...@googlegroups.com
On 8/01/2013 10:03pm, joy wrote:
> I have the same problem, the tutorial speaks about a list of things to do:
>
> * Push your code up to the deployment server.
> * On the server, run collectstatic
> <https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#django-admin-collectstatic>
> to copy all the static files into STATIC_ROOT
> <https://docs.djangoproject.com/en/1.4/ref/settings/#std:setting-STATIC_ROOT>.
> * Point your web server at STATIC_ROOT
> <https://docs.djangoproject.com/en/1.4/ref/settings/#std:setting-STATIC_ROOT>.
>
> I have arrived at the second point. But the static files are not served,
> i have set up the STATIC ROOT and the STATIC_URL, but what should i add
> to the template src tag to find the pages? The tutorial speaks about the
> template context processor, but it doesn't exist in my setting.py file.
>
> TEMPLATE_CONTEXT_PROCESSORS = (
> 'django.core.context_processors.debug',
> 'django.core.context_processors.i18n',
> 'django.core.context_processors.media',
> 'django.core.context_processors.static',
> 'django.contrib.auth.context_processors.auth',
> 'django.contrib.messages.context_processors.messages',
> )
>
> i cannot make my css and javascript work, please, can you explain me what is happening behind the interface?
> Agnese
>
>
> Il giorno sabato 5 gennaio 2013 19:38:32 UTC+1, MÄ rtiņš JakuboviÄ s
> ha scritto:
>
> Hello.
>
> I try a lot of things and can't understand, why not working
> STATIC_ROOT and MEDIA_ROOT in settings.py.
>
> I want, that all my media and static folders is in different place
> that python files, so I set media, static and templates to different
> place. Templates TEMPLATE_DIRS works well, bet MEDIA_ROOT
> and STATIC_ROOT not.
>
> I try setup like this:
>
> /home/domain/www/my_proj/ there is project folder and all apps, and
> there I place my htdocs folder, in which is static, templates and
> media folders.
>
> When i set in STATIC_ROOT = '/home/domain/www/my_proj/htdocs/static'
>
> In apache error log i got:
>
> File does not exist: /home/domain/www/my_proj/my_proj/static
>
> I don't get, why django don't want to take new setting...
>
> I use django 1.4.

I think you need to set up Apache to serve the static and media files.
Here is how mine works ...

# lock the public out
<Directory /var/www/my_proj/>
AllowOverride None
Order deny,allow
Deny from all
</Directory>

# serve uploaded media from here
<Directory /var/www/media/my_proj/>
AllowOverride None
Order deny,allow
Allow from all
</Directory>

# serve static stuff from here
<Directory /var/www/static/my_proj/>
AllowOverride None
Order deny,allow
Allow from all
</Directory>

<IfModule mod_alias.c>
Alias /media/ /var/www/media/my_proj/
Alias /static/ /var/www/static/my_proj/
Alias /tiny_mce/ /var/www/static/my_proj/js/tiny_mce/
Alias /jquery/ /var/www/static/my_proj/js/jquery/
</IfModule>

Then in your template you can use {{ STATIC_URL }}/whatever

Hope this helps

Mike

>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/fwNEyOLpFb4J.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Agnese Camellini

unread,
Jan 8, 2013, 6:25:09 AM1/8/13
to django...@googlegroups.com
I'm not using apache now, i'm just using the development server into django.
Why is it so difficult to serve static files?
joy

2013/1/8 Mike Dewhirst <mi...@dewhirst.com.au>

    Thanks.

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

Jonathan

unread,
Jan 8, 2013, 9:19:40 AM1/8/13
to django...@googlegroups.com
Assuming that you have settings.STATIC_ROOT configured, you can use the following snippet to handle URL routing for static files with the dev server:

# urls.py
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
    )

Feel free to add a similar pattern for media files as well.

Hope this helps,
Jonathan
2013/1/8 Mike Dewhirst <mi...@dewhirst.com.au>

    Thanks.

For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Agnese Camellini

unread,
Jan 8, 2013, 10:46:19 AM1/8/13
to django...@googlegroups.com
I've foud a tutorial, finally.


Interesting, but it speaks about an older version of Django.

Thank you all for the answers.

2013/1/8 Jonathan <jonathand...@gmail.com>

jianhui chen

unread,
Jan 8, 2013, 11:52:47 AM1/8/13
to django...@googlegroups.com
Hi,
   I am not to interrupt your question, actually I have the same problems as you. It seems that Django comers suffer this problem so much.
   I use Manager Static File as a guide, but still has problem.
   1, Put your static files somewhere that staticfiles will find them
      I set following in the setting.py
      STATIC_URL = '/static/'

     # Additional locations of static files
    STATICFILES_DIRS = (
       # Put strings here, like "/home/html/static" or "C:/www/django/static".
       # Always use forward slashes, even on Windows.
       # Don't forget to use absolute paths, not relative paths.
       "E:/code/python/djangoBook/django-testapp-develop/static"  #this is only used in my local test
      )
   2, Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
      So I set that: 
       INSTALLED_APPS = (
#    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.staticfiles',   #this
    'djangotoolbox',
    'autoload',
    'dbindexer',
    'shape_practice',

    # djangoappengine should come last, so it can override a few manage.py commands
    'djangoappengine',
   
)
       TEMPLATE_CONTEXT_PROCESSORS = (
         'django.contrib.auth.context_processors.auth',
       'django.core.context_processors.request',
       'django.core.context_processors.media',
        'django.core.context_processors.static',  #this
     )
  3, You’ll probably need to refer to these files in your templates.
   So I did this:

  <img src="{{ STATIC_URL }}images/gauge_example.jpg" alt = "aa gauge example" width = "390" height = "225" />
  
  And, so someone pointed out that : using RequestContext, so I dit that
  def introduction(request):
    t = loader.get_template('introduction.html')   
    c = RequestContext(request, {})   #this
    return HttpResponse(t.render(c))   

  The problem is that when I comment out "E:/code/python/djangoBook/django-testapp-develop/static" in the STATICFILES_DIRS , it has an error :
"NetworkError: 500  - http://127.0.0.1:8000/static/images/gauge_example.jpg". When I didn't commented out, it works. So I think STATICFILES_DIRS and STATIC_URL actually works in 
my app, the problem is that how to set it to the correct path. In my case, I want to STATIC_URL can find my "static" folder which is in the project home directory.
  I was puzzled by this problem many days, right now I have no way but to use local folder "E:/code/python/djangoBook/django-testapp-develop/static" to code and test in my PC.
  Thanks.

Jianhui
 

 

     


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/crL9a25SIWMJ.
Reply all
Reply to author
Forward
0 new messages