views:

331

answers:

3

I am in the process of migrating an application (Sage) from Twisted to Django.

Static documentation is currently served under /doc/static, while live (built on-the-fly) documentation are served under /doc/live.

Is it possible to use Twisted to serve /doc/static only, leaving Django to serve the rest of /doc/*?

A: 

Unless I misunderstood the question, why not simply rewrite the /doc/static url to Twisted before it even reaches Django (ie. at the Apache / proxy level)?

http://httpd.apache.org/docs/2.0/mod/mod%5Frewrite.html

pithyless
We use Twisted to serve our files, with no other layer of abstraction -- at least for local installations. Thus using ``mod_rewrite`` is impossible.
Tim Dumol
+1  A: 

It can be done, the degree of elegance just varies... I understand this is for transition, so it might not have to be very beautiful.

If you have to have Twisted serving the static files, then you either have to hack together in django a proxy-through for those files, or throw something in front of the whole thing. Also Perlbal with VPATH could do this, it'll take regexp's of urls and make them hit the right services.

If you don't have to use Twisted, there are lots of different ways to do it. You could still use Perlbal or something similar to serve static files, something you should have anyway in the long run.

af
We include Twisted as a convenient way to host the Sage Notebook, so the second option is out of the question.Proxying the requests from Django to Twisted seems plausible, but it seems that it nullifies the performance boost by letting Twisted serve them directly. I'll run a few benchmarks. Thanks!
Tim Dumol
A: 

Have a look at this link on how to run Django on top of Twisted: (instructions copied from the blog)

  1. easy_install Twisted
  2. easy_install Django
  3. Profit!
  4. django-admin.py startproject foo
  5. Create a myapp.py with the following code:

    from django.core.handlers.wsgi import WSGIHandler

    application = WSGIHandler()

  6. export DJANGO_SETTINGS_MODULE=foo.settings

  7. twistd -no web --wsgi=myapp.application

Further down in the comments there is also an example of how to serve media directly with Twisted before the request is passed on to Django:

To handle media files just use "static.File" from "twisted.web" like so: staticrsrc = static.File(os.path.join(os.path.abspath("."), "mydjangosite/media")) and then add that resource to your root resource like so: root.putChild("media", staticrsrc)

Disclaimer: I have not tried this myself, but the blog article seems quite recent and the author willing to respond to questions.

EDIT: There is also another article written on the subject with instructions on how to get it working here, which seems to include servering static files with Twisted directly.

Andre Miller