I am trying to validate a form, such that if IP of user (request.META['REMOTE_ADDR']
) is in a table BlockedIPs
, it would fail validation. However I don't have access to request
variable in Form
. How do I do it? Thanks.
views:
292answers:
1
+9
A:
Make it available to your form by overriding __init__
so it can be passed in during construction (or you could just pass the IP itself):
from django import forms
class YourForm(forms.Form)
# fields...
def __init__(self, request, *args, **kwargs):
self.request = request
super(YourForm, self).__init__(*args, **kwargs)
# validation methods...
Now you just need to pass the request
object as the first argument when initialising the form and your custom validation methods will have access to it through self.request
:
if request.method == 'POST':
form = YourForm(request, request.POST)
# ...
else:
form = YourForm(request)
# ...
insin
2009-02-18 10:22:58
thanks, that's exactly what I was looking for
pitr
2009-02-18 10:25:07