在 Django 的 models.py 中,如果我正在验证表单,如何获取用户的 IP?
我知道如何在views.py中获取它......
request.META['REMOTE_ADDR']
但是,当我的其中一个表单正在验证时,如何在models.py中获取它?
I know how to get it in views.py....
request.META['REMOTE_ADDR']
However, how do I get it in models.py when one of my forms is being validateD?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将请求对象传递给正在调用的表单/模型代码:然后,这将提供对 request.META['REMOTE_ADDR'] 的访问。或者,只需将其传递进去即可。
You can pass the request object to the form/model code that is being called: this will then provide access to
request.META['REMOTE_ADDR']
. Alternatively, just pass that in.一个可能的方法,但我不确定它是否是最好的...
定义你自己的干净方法,
Ona possible way, but i am not sure if it is the best or not...
define your own clean method,
如果您在表单级别或模型级别进行验证,则两个实例对 HTTP 请求(存储客户端 IP 信息的位置)一无所知。
我可以想到两个选项:
If you are validating at form level or at model level, both instances know nothing about the HTTP request (where the client IP info is stored).
I can think of two options: