In Django, is it possible to set the default value of a form field to be tied to a request variable. e.g. request.META['REMOTE_ADDR'].
This is just an example, it’d like to be use any of the headers available in the request variable without passing it explicitly to the form.
Thanks.
No, it is not possible, since the request object is not globally available, and thus it is not available in the form unless explicitly passed in. The
initialargument exists for exactly the problem you are trying to solve. You should probably usein your view.
Edit:
If you like to pass in the
requestexplicitly, you can use something like this, and then just passrequest=requestwhen you instantiates the form: