Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm new to web servers, so please be gentle:

I've set up a new apache2 server on a Mint System (Ubuntu-base, which also means Debian-based). I've also installed a Django backend and the wsgi_mod extension. I'm just serving from localhost for testing purposes.

The Django admin interface (accessible with localhost/admin/) refers to its css file with a relative URL like static/admin/base.css. Now I see in my apache logs that the browser is trying to fetch it under the full URL localhost/admin/static/admin/base.css, which doesn't exist.

I was under the impression that relative paths are always resolved by appending them to the basename, not the current URL. (Which would lead to localhost/static/admin/base.css, which is where the file actually IS).

Have I thought wrong (and my Django settings are mixed up), or have I miss-configured my apache server? The following would be my 000-default file in site-enabled folder:

<VirtualHost *:80>
    ServerName localhost
    ServerAlias 127.0.1.1
    ServerAdmin webmaster@localhost

    DocumentRoot "/home/web/http/80/localhost/htdocs"
    ErrorLog "/home/web/http/80/localhost/logs/error.log"
    CustomLog /home/web/http/80/localhost/logs/access.log combined
    LogLevel warn

    WSGIScriptAlias / /home/web/http/80/localhost/lib/app.wsgi

    Alias /robots.txt /home/web/http/80/localhost/htdocs/robots.txt
    Alias /favicon.ico /home/web/http/80/localhost/htdocs/favicon.ico
    Alias /images /home/web/http/80/localhost/htdocs/images
    Alias /static /home/web/http/80/localhost/htdocs/static    


    <Directory /home/web/http/80/localhost/htdocs/>
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

</VirtualHost>

I haven't changed anything in apache2.conf, ports.conf, envar or wsgi.conf. For testing purposes, my .htaccess file is also empty.

The relavant (?) part of my settings.py looks like this:

STATIC_ROOT = '/home/web/http/80/localhost/htdocs/static/'
STATIC_URL = 'static/'
# Additional locations of static files
STATICFILES_DIRS = ()
ROOT_URLCONF = 'osmmap.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'osmmap.wsgi.application'

while this is my urls.py:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:    
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:

    url(r'^test/', 'testapp.views.home'),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You want your static path to be host relative:

STATIC_URL = '/static/'
share|improve this answer
    
This sounds promising. A leading / will put the path right after the hostname? So now just for curiosity: can I influence what is used as the host name? Something equivalent of chroot for the webserver? (e.g. setting localhost/mysite as the host name, so /static/ would resolve to localhost/mysite/static/) –  Chaos_99 Feb 12 '13 at 7:35
    
Not automatically as far as I know, you'd have to include that in STATIC_URL. –  mgorven Feb 12 '13 at 17:12
    
Now checked and it (the original problem) indeed works as intended. Thanks! –  Chaos_99 Feb 12 '13 at 18:15
add comment

I was under the impression that relative paths are always resolved by appending them to the basename, not the current URL.

This is correct.

(Which would lead to localhost/static/admin/base.css, which is where the file actually IS).

This is not correct.

Relative URLs are constructed by stripping everything following the final / from the base URL and then appending the relative URL. So http://localhost/admin/ has nothing after the final / and would have nothing stripped before adding static/admin/base.css.

Therefore, the constructed URL http://localhost/admin/static/admin/base.css is constructed correctly.

You will need to have the application developer fix this problem.

share|improve this answer
    
Thanks for the clarification. Seems my understanding of 'base name' was wrong. Unfortunately I AM the application developer. –  Chaos_99 Feb 10 '13 at 20:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.