first you configure in settings.py the variables STATIC_ROOT and STATIC_URL, then configure http server accordingly
STATIC_ROOT is the absolute path in filesystem that stores your static files, you can put that inside django project itself if it is convenient
and then in apache use Alias and Directory accordingly
STATIC_ROOT = '/srv/django/static_files'
STATIC_URL = '/django_static/'
then in apache :
ALias /django_static/ /srv/django/static_files/
<Directory /srv/django/static_files>
Require all granted
</Directory>
On Friday, March 29, 2013 10:36:30 PM UTC+3, David Pitchford wrote:
I am experienced with Python but new to Django and web development in general. I am struggling to understand its static files system from the documentation. It seems like I have to set multiple settings variables and create multiple folders in order to get the server to accomplish the simple task of "finding" these files. After toying with it for a few hours I haven't been able to get the static files system to work and and have resorted to the following system which is probably a very bad idea:--In views.py:from django.http import HttpResponsefrom django.shortcuts import render_to_responsefrom django.template import RequestContextimport datetimeimport os.pathimport settingsstatictypes = {".css": "text/css",".js": "text/javascript"}def servestatic(request, filename):fullfilename = os.path.join(settings.STATIC_ROOT, filename) ext = os.path.splitext(filename)[1]return HttpResponse(open(fullfilename).read(), content_type=statictypes[ext]) And in urls.py:from django.conf.urls import patterns, include, urlimport mysite.views as viewsstaticextensions = [ext[1:] for ext in views.statictypes.keys()]staticextstring = '|'.join(staticextensions)urlpatterns = patterns('',...(r"([^/]+\.(?:%s))$" % staticextstring, views.servestatic))This actually works (and I could optimize it by caching the static file contents in memory rather than continually rereading them), but of course it's circumventing Django's built-in system for managing static files. My project architecture looks like this:mysite||--manage.py|--mysite||__init__.py|settings.py|urls.py|views.py|wgsi.py|--static| || |--jquery.js| |--TestFormat.css||--templates||--TestTemplate.htmlAt the beginning, the documentation page mentions, "For small projects, this isn't a big deal, because you can just keep the static files somewhere your web server can find it." This sounds like the simple solution I'm looking for; what does it mean and how do I do it? I'm also frequently confused by how when I created the project it created two nested folders with the same name. Which is considered to be the "project root"?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment