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 l开发者_高级运维ike 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?
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>
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
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.
精彩评论