简体   繁体   English

在Django的models.py中,如果我正在验证表单,如何获取用户的IP?

[英]Inside Django's models.py, if I am validating a form, how do I get the user's IP?

I know how to get it in views.py.... 我知道如何在views.py中获取它。

request.META['REMOTE_ADDR'] request.META ['REMOTE_ADDR']

However, how do I get it in models.py when one of my forms is being validateD? 但是,当我的表单之一为validateD时,如何在models.py中获取它?

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'] . 您可以将请求对象传递给正在调用的表单/模型代码:然后将提供对request.META['REMOTE_ADDR'] Alternatively, just pass that in. 或者,只需将其传入。

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). 如果要在表单级别或模型级别进行验证,则两个实例都不知道HTTP请求(存储客户端IP信息的位置)。

I can think of two options: 我可以想到两种选择:

  • Validate at the view level where you can insert errors into the form error list. 在视图级别进行验证,您可以在其中将错误插入到表单错误列表中。
  • You can put the user IP (may be encrypted) in a hidden field at your form. 您可以将用户IP(可能已加密)放在表单的隐藏字段中。

Ona possible way, but i am not sure if it is the best or not... 一种可能的方法,但是我不确定这是否是最好的方法。

define your own clean method, 定义自己的清洁方法,

class someForm(forms.Form):
    afield = CharField()

    def clean(self, **kwargs):
        cleaned_data = self.cleaned_data
        afield = cleaned_data.get('afield')
        if 'ip' in kwargs:
            ip = kwargs['ip']
            # ip check block, you migth use your cleaned data in here
        return cleaned_data


some_info = {'afield':123} #you will wish to use post or gt form data instead, but tihs iis for example
form = someForm(some_info) 
if form.is_valid():
    data = form.clean({'ip':request.META['REMOTE_ADDR']}) # you pass a dict with kwargs, which wwill be used in custom clean method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何在django models.py上关联用户模型 - how should I relate user models on django models.py 如何在 models.py 中获取当前用户? - How can I get the current user in models.py? 如何将其他应用程序的 models.py 中的新字段添加到 Django、Python 中的 html - How can I add a new field from the other app's models.py to html in Django, Python 如何查看 django 中的 models.py? - How I can give a view to my models.py in django? Django如何在models.py中获取用户名? - Django how to get username in models.py? 如何在我自己的 python 程序中使用 django models.py 类? - How do I use django models.py classes in my own python programs? 使用django.contrib.auth.models导入用户时,是否需要在models.py中创建单独的类? - Do I need to create a separate class in my models.py when using the django.contrib.auth.models import user? 在django中将许多模型创建到文件models.py中是正确的 - In django It's correct create much models into file models.py 如何为以下 models.py 和 serializers.py 创建 REST 视图集? 我是 django rest 框架的新手 - How to create REST viewset for following models.py and serializers.py? i am new with django rest framework 在models.py文件中获取当前登录的Django用户? - Get the currently logged in Django user in a models.py file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM