Tag Archive | "python"

How to Get a Client IP Address in DJANGO

Friday, August 14, 2009

5 Comments

Here is a small snippet to get the client’s ip address in your python code in DJANGO. You might have tried the ‘REMOTE_ADDR’ key in the request object but it always returns 127.0.0.1. To fix this, you can use the ‘HTTP_X_FORWARDED_FOR’ variable like so: from django.http import HttpRequest def mypage(request): client_address = request.META['HTTP_X_FORWARDED_FOR'] .... The […]

Continue reading...

Redirecting to a 404 page in Django

Saturday, April 4, 2009

Comments Off on Redirecting to a 404 page in Django

There are a few ways to redirect to a 404 page (or page not found error) in Django. The easiest way is to raise Django’s built in 404 exception, like so: from django.http import Http404 def myView(request, param): if not param: raise Http404 return render_to_response('myView.html') You can add a file called 404.html to your templates […]

Continue reading...

How to Sleep, Pause, Wait, or Stop your Python Code

Saturday, March 14, 2009

3 Comments

Here is the function to pause or stop your code in Python for a variable amount of time: PLAIN TEXT PYTHON: span style="color: #808080; font-style: italic;">#will sleep for 5 seconds #will sleep for half a second #will sleep for a tenth of a second This is useful if you are making numerous calls to web […]

Continue reading...