Django - routing to non-django script? (e.g. simple WSGI/CGI application)

寵の児 提交于 2019-12-23 04:45:36

问题


How (if it is possible) can set up a route in Django that would point a certain url to a non-django script? E.g. I'd like to have /independent handled by a very simple CGI script such as this one:

import os

print 'Status: 200 OK'
print 'Content-type: text/html'
print
for key, value in os.environ.items():
    print key, ': ', value, '<br/>'

I'm not a Django user (yet, I.. think, it's upon me) and just need a way to hack this little detour into an application. So: is this possible? If yes, how?


回答1:


Not directly. All Django urls have to go to a Django view, a callable which accepts a request and returns a response.

You can though return a redirect[1] to an arbitrary urls. So you should be able to write a very simple wrapper view to do what you want.

[1] https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#examples




回答2:


If you have permissions to change your Apache conf, you can just add a line above the bit that handles Django. (Assuming you run your Django via WSGI.) Mine is something like

<Directory "/cgi-bin/">
    Options +ExecCGI
    AddHandler cgi-script .cgi .py
</Directory>

ScriptAlias /cgi-bin/ /path/to/cgi/dir/

...

WSGIScriptAlias / /path/to/django/site/django.wsgi

<Directory /path/to/django/site>
Order deny,allow
Allow from all
</Directory>



回答3:


Another possibility to the ones mentioned by Valkyrie and amjoconn is to handle it in your .htaccess file or similar server setup. For example, on my WebFaction account, I have Django handling everything except /media/ and /downloads/. Those two directories are served directly by the nginx server.



来源:https://stackoverflow.com/questions/6664678/django-routing-to-non-django-script-e-g-simple-wsgi-cgi-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!