当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Serving static files with Twisted and Django under non-root folders

问题描述:

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/*?

网友答案:

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.

网友答案:

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.

网友答案:

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_rewrite.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: