Re: Confusion about Static Files

2013-04-01 Thread yakoub abaya
it is very simple and doesn't require a separate http server like nginx 
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/

  Require all granted


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 HttpResponse
> from django.shortcuts import render_to_response
> from django.template import RequestContext
> import datetime
> import os.path
> import settings
>
> statictypes = {".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, url
> import mysite.views as views
>
> staticextensions = [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.html
>
> At 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+unsubscr...@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.




Re: Confusion about Static Files

2013-04-01 Thread David Pitchford
These differences between debug and production mode are also a bit 
confusing. The site I am writing is *not* supposed to be very scalable; it 
is a very simple web application for internal use at my job that we aren't 
expecting to have more than a few users. It would only take me a few hours 
to code it in PyGTK, but we want to put it online so remote users can 
access the same database. It are only supposed to have a few pages and a 
few more static files, so setting up multiple servers seems unjustified. I 
will see if I can use a more standard solution for static files (I think it 
may be running on Apache), but if not, is there a more elegant way to serve 
static files through Django in production?

On Friday, March 29, 2013 4:02:30 PM UTC-5, BFSchott wrote:
>
> David,
>
> This took me a while to figure out also when I first started.  Running 
> with debug on and runserver works great, then you try to use it in 
> production and things break.  The big picture idea is that static files are 
> intended to be hosted by something in front of Django, be that Apache or 
> Ngnix or whatever.  All of the django.contrib.static module does is define 
> a) how/where the manage.py collectstatic command finds static files, 
> STATICFILES_FINDERS, STATICFILES_DIRS, b) where it will save them on the 
> local filesystem STATIC_ROOT, and c) what URL prefix will get used in 
> templates STATIC_URL.
>
> With settings.DEBUG  = True, you can add static files to your url pattern 
> with the code below so that your server will work with manage.py runserver, 
> but as soon as you go production and turn off DEBUG, this will stop working 
> by design.
> ---
> from django.contrib.staticfiles.urls import staticfiles_urlpatterns
> # ... the rest of your URLconf goes here ...
> urlpatterns += staticfiles_urlpatterns()
> ---
>
> You really want not to serve files through Django.  Just do a 
> collectstatic before runserver, and point your web server /static/ location 
> at that directory. Here is a link to the nginx.conf file used by Mezzanine 
> (a Django CMS app).
> https://github.com/nimbis/mezzanine-project/blob/master/deploy/
> Notice that location /static/ is served directly and everything else for 
> the most part is a redirect to the Django server running on another port. 
>  You can find Apache examples all over the place.  
>
>
> Brian Schott
> bfsc...@gmail.com 
>
>
>  
> On Mar 29, 2013, at 3:36 PM, 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 HttpResponse
> from django.shortcuts import render_to_response
> from django.template import RequestContext
> import datetime
> import os.path
> import settings
>
> statictypes = {".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, url
> import mysite.views as views
>
> staticextensions = [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.html
>
> At 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 
> 

Re: Confusion about Static Files

2013-03-29 Thread Brian Schott
David,

This took me a while to figure out also when I first started.  Running with 
debug on and runserver works great, then you try to use it in production and 
things break.  The big picture idea is that static files are intended to be 
hosted by something in front of Django, be that Apache or Ngnix or whatever.  
All of the django.contrib.static module does is define a) how/where the 
manage.py collectstatic command finds static files, STATICFILES_FINDERS, 
STATICFILES_DIRS, b) where it will save them on the local filesystem 
STATIC_ROOT, and c) what URL prefix will get used in templates STATIC_URL.

With settings.DEBUG  = True, you can add static files to your url pattern with 
the code below so that your server will work with manage.py runserver, but as 
soon as you go production and turn off DEBUG, this will stop working by design.
---
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
---

You really want not to serve files through Django.  Just do a collectstatic 
before runserver, and point your web server /static/ location at that 
directory. Here is a link to the nginx.conf file used by Mezzanine (a Django 
CMS app).
https://github.com/nimbis/mezzanine-project/blob/master/deploy/
Notice that location /static/ is served directly and everything else for the 
most part is a redirect to the Django server running on another port.  You can 
find Apache examples all over the place.  


Brian Schott
bfsch...@gmail.com



On Mar 29, 2013, at 3:36 PM, 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 HttpResponse
> from django.shortcuts import render_to_response
> from django.template import RequestContext
> import datetime
> import os.path
> import settings
> 
> statictypes = {".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, url
> import mysite.views as views
> 
> staticextensions = [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.html
> 
> At 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+unsubscr...@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.
>  
>  

-- 
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+unsubscr...@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.




Confusion about Static Files

2013-03-29 Thread David Pitchford
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 HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
import datetime
import os.path
import settings

statictypes = {".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, url
import mysite.views as views

staticextensions = [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.html

At 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+unsubscr...@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.