0

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)),
)

2 Answers 2

0

You want your static path to be host relative:

STATIC_URL = '/static/'
3
  • 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, 2013 at 7:35
  • Not automatically as far as I know, you'd have to include that in STATIC_URL.
    – mgorven
    Feb 12, 2013 at 17:12
  • Now checked and it (the original problem) indeed works as intended. Thanks!
    – Chaos_99
    Feb 12, 2013 at 18:15
0

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.

1
  • Thanks for the clarification. Seems my understanding of 'base name' was wrong. Unfortunately I AM the application developer.
    – Chaos_99
    Feb 10, 2013 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .