0

I'm trying to work through the django tutorial but have gotten hung up on displaying the css associated with the admin module. I'm working on windows, with django 1.6, apache and MySQL

My current hierarchy is as follows:

MySite
|
|---static
|   |--admin
|      |--css
|      |--img
|      |--js  
|---MyApp
|   |--__init__.py
|   |--admin.py
|   |--models.py
|   |--tests.py
|   |--views.py
|---MySite
|   |--__init.py
|   |--settings.py
|   |--testdb.py
|   |--urls.py
|   |--wsgi.py
|--manage.py

within the settings.py file I've defined the STATIC_URL as

STATIC_URL = '/MySite/static/'

When I inspect the resulting admin page it shows that it is looking for the css at MySite/static/admin/css, which is where it is, but its not finding it there. It seems the root isn't correct.

In my urls.py I've got

admin.autodiscover()

urlpatterns = patterns('',  
    url(r'^admin/', include(admin.site.urls)),
)

EDIT:

My Apache htttpd.conf file is completely untouched from what XAMPP originally installed, except I've included *LoadModule wsgi_module modules/mod_wsgi.so* at the appropriate spot, and appended

WSGIScriptAlias /MySite F:/Web_Django/MySite/MySite/wsgi.py
WSGIPythonPath F:/Web_Django/MySite
<Directory F:/Web_Django/MySite/MySite/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

I've included the entire file at http://pastebin.com/AD77f69c


EDIT2:

I've discovered that when using apache you need to implement the wsgi.py file slightly differently. I now have the following, but the problem remains.

import os
import sys

path = 'f:/web_django/Nutana/'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'Nutana.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Graham Dumpleton, the developer of the modwsgi adaptor for apache, provides an alternate wsgi file which he (and many others, apparently) feels works better in an old blog post (http://tinyurl.com/y8wr7gc), but it doesn't work for me.

marcp
  • 1,179
  • 2
  • 15
  • 36
  • try to remove the `/MySite/` from `STATIC_URL` to be just `STATIC_URL = '/static/'` – Aamir Rind Dec 17 '13 at 19:22
  • Nope, that doesn't work either. In that case the apparent path listed on the resources tab of the chrome developer tools is /static/admin/css/base.css with a 404 result. The path to the html file is listed as /MySite/admin/ – marcp Dec 17 '13 at 19:29
  • I predict it's actually looking in `MySite/MySite/static/admin/css`. Put it there and see if you find it. – Rob L Dec 17 '13 at 19:52
  • Sorry @RobL, that doesn't work either. – marcp Dec 17 '13 at 19:55
  • 1
    Don't try and work through the tutorial via Apache. There's a reason it tells you to use the development server. Follow its recommendations, use the dev server, learn how it works, and *then* learn how to deploy with Apache. – Daniel Roseman Dec 17 '13 at 22:15
  • @DanielRoseman, I'm sure you are right and that is the path of least resistance, however I prefer to get everything right from the start and it seems migrating to Apache is a major hurdle when one builds against the dev server. For the purpose of this discussion you could replace "work through the django tutorial" with "build my first website" and the identical problem would remain. – marcp Dec 18 '13 at 16:02
  • Well, with all due respect to the august Mr Dumpleton, that article is more than three years old, and quite a few improvements have been made to Django in the meantime. In particular, the project layout has changed, and runserver does a lot less hacking now than it used to (so, for example, settings are no longer imported twice). But the bigger point is that you then have to actually configure Apache to serve the static files, which you haven't done. – Daniel Roseman Dec 18 '13 at 16:06

2 Answers2

1

EDIT

It is, of course better to have the customization for the project within the project itself, locally at least. This anticipates the use of collectstatic to prepare for moving the site off of my development machine. Following @LGama's advice I've moved it into the project and updated the alias as shown below.


@DanielRoseman commented that I "have to actually configure Apache to serve the static files", which of course I thought I was doing! By going back through the apache error log files, and consulting this similar question (Apache not serving django admin static files) I've sussed it out. There needs to be a 'static' alias created in httpd.conf. I've also moved the wsgi.py script into its own folder upon the advice of Graham Dumpleton. So now my structure is

f:\django_web/
   |---MySite
       |---static/
       |   |---admin/
       |       |--css
       |       |--img
       |       |--js  
       |---MyApp/
       |   |--__init__.py
       |   |--admin.py
       |   |--models.py
       |   |--tests.py
       |   |--views.py
       |---MySite/
       |   |--__init.py
       |   |--settings.py
       |   |--testdb.py
       |   |--urls.py
       |---Apache/
       |   |--wsgi.py
       |--manage.py

and my httpd configuration includes:

Alias /static "F:/Web_Django/MySite/static/"
<Directory "F:/Web_Django/MySite/static/">
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>

WSGIScriptAlias /MySite F:/Web_Django/MySite/Apache/wsgi.py
WSGIPythonPath F:/Web_Django/MySite
<Directory F:/Web_Django/MySite/Apache/>
Order deny,allow
Allow from all
</Directory>

and everything works!

Community
  • 1
  • 1
marcp
  • 1,179
  • 2
  • 15
  • 36
1

Just to clarify your answer: you can keep your files inside the project (for development purposes) and when you need deploy you utilize the command

django-admin.py collectstatic¶

to collect your files in the directory described in settings.py > STATIC_ROOT

One of the vantages is that you can use your JS and CSS normally and when its time to deploy you can use an extension like compression (http://django-compressor.readthedocs.org/en/latest/) to minify and send the minified to the static root (that can be in a CDN).

Further reading: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

EDIT

In Nginx you would use

location /myuser/static/ {                                         # STATIC_URL
    alias /home/myuser/projects/src/MyProject/static/;              # STATIC_ROOT
}

to define an alias to the STATIC_ROOT.

The server would then know where to look for those files created with Collect_static.

What I believe happens with you is that your server doesn't know where to find your files inside the project. Other problem with using files served inside the project is that you lose modularity. you cant have different static directories (one for each module) inside your project without having to create more aliases in your server (more clutter).

That doesnt happen with the dev server by the way. it finds your files without need to configuration.

LGama
  • 561
  • 4
  • 10
  • Because you need to have a static repository to serve your files. Apache looks for the directory configured in the conf – LGama Dec 18 '13 at 20:14
  • I think that's what I described in my answer isn't it? When I had the /admin folder beneath MySite I couldn't get that to work. Or am I being dense? – marcp Dec 18 '13 at 20:18
  • you're not being dense i also spent quite some time with this problem. Edited my answer to improve some. – LGama Dec 18 '13 at 20:40
  • OK I've moved my static folder into the MySite directory and changed the apache '/static' alias to point at it and this also works. I'd swear I tried that earlier. But I'm not sure how I'm further ahead this way. – marcp Dec 18 '13 at 21:29
  • 1
    not in any way if you are using raw sources in your static files. If you use any kind of compressing or caching in your project you got the option do it without affecting your sources and can use CDNs as well. – LGama Dec 18 '13 at 21:55