I am a newbie please be gentle. I am using Nitrous.io and Django 1.5. I am experiencing some weird occurrences and was looking for some explanation. I am not sure if it is a piece of Middle-ware or my newbism that is causing problems (my money is on my newbism). Sorry for the blue squiggles I was not sure what the policy is on this sorta thing.
This is my urls.py:
urlpatterns = patterns('',
url(r'^church$', church),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Before you tell me that I do not need to add that static snippet please note that is the only way I could get my static files to be properly served.
This is the relevant portions of my settings.py:
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'StructureBuilder', #this is the name of my app
'django.contrib.admin',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
STATIC_URL = 'MySite/static/'
Before you tell me that my STATIC_URL
is wrong or that I do not need the STATIC_ROOT
, please note this is the only way I have been able to get my static files to be properly served.
This is what happens:
If I add a forward slash to my urlpatterns for example url(r'^church/$', church),
the regex checks out however none of my static files are loaded.
If I remove the forwards slash (eg. url(r'^church$', church),
and reload the page, I will get a 404 error because of the forward slash. I was under the impression that common middle-ware (which I have installed) handled the forward slash Link
Removing the forward slash loads the page perfectly:
Obviously, this is not desired. I can work with it, but this happens with any forward slash not just when it is at the end of the regex. This becomes a problem because it will also add the current page to the beginning of all my static files.
My Questions: Why is the common middle-ware not properly handling the forward slash? What exactly is wrong with my static file serving configuration that the above way is the only way I am able to get it to work?
With no slash in front of your STATIC_URL
the browser is treating it as a relative URL.
As you can see in your screenshot of normalize.css
failing /church/ is prefixed to your STATIC_URL
. Where in the other requests where these files are working, /church/ is not there.
Change:
STATIC_URL = 'MySite/static/'
to
STATIC_URL = '/MySite/static/'
This will cause the requests of the static content to never be influenced by slashes in your URL as they will always start @ the root of the server.