简体   繁体   English

为什么我的函数返回对象标识而不是'ip'变量?

[英]Why is my function returning the object identity instead of the 'ip' variable?

Upon saving User sign up form, I expect to save an ip address of the user, but my view function won't return a string . 保存用户注册表单后,我希望保存用户的ip地址,但是我的视图函数不会返回string I get the object identity of the get_client_signup_ip function instead: 我改为获取get_client_signup_ip函数的对象标识:

<function get_client_signup_ip at 0x04461810>

forms.py: forms.py:

    from django import forms
from captcha.fields import ReCaptchaField

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

from django.http import HttpRequest

from django.contrib.gis.geoip2 import GeoIP2

from . import views

from .models import CustomUser

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)
    captcha = ReCaptchaField()

    class Meta:
        model = CustomUser # this makes the UserCreateForm always save data to the custom user model
        fields = ("username", "email", "password1", "password2")


    def save(self, commit=True, request=True): # user object (customuser) form is called to save with commit=true, so it gets teh ip and saves.
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        user.origin_ip = views.get_client_signup_ip(request)
        if commit:
            user.save()
        return user

views.py views.py

def get_client_signup_ip(request):
    g = GeoIP2()
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for
        ip2 = '192.227.139.106'
        city = g.city(ip2)
    else:
        ip = request.META.get('REMOTE_ADDR')
        ip2 = '192.227.139.106'
        city = g.city(ip2)

    return ip

urls.py urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('signup/', views.SignUp.as_view(), name='signup'),
    path('signup/', views.get_client_ip, name='signup_ipaddress')

]

I expect to see an ip address in the User's origin_ip field. 我希望在用户的origin_ip字段中看到一个IP地址。 Instead I get an string representation for the get_client_sign_up function. 相反,我得到了get_client_sign_up函数的字符串表示形式。

How can I get my view function get_client_sign_ip to return a string? 如何获取视图函数get_client_sign_ip以返回字符串?

To get the value you need to call it as method views.get_client_signup_ip() instead of views.get_client_signup_ip . 要获取该值,您需要将其称为views.get_client_signup_ip()方法而不是views.get_client_signup_ip

user.origin_ip = views.get_client_signup_ip(request)

Also, for simply getting ip from a function you don't need the HttpResponse(ip) from a method. 另外,仅从函数获取ip即可,而无需从方法获取HttpResponse(ip)

def get_client_signup_ip(request):
    ..........

    return ip

暂无
暂无

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

相关问题 为什么我的 function 返回 None 而不是我的键值 - Why is my function returning None instead of my key values 为什么我的代码返回变量的地址而不是值? - Why is my code returning the address of the variable instead of the value? 为什么我的 Selenium xpath 表达式返回一个 [object attribute] 而不是一个元素? - Why is my Selenium xpath expression returning an [object attribute] instead of an element? Django:我的函数正在返回一个对象而不是返回值 - Django: My function is returning an object instead of the return value 为什么我的 Apriori function 返回的是字母而不是项目? (错误的输出) - Why is my Apriori function returning letters instead of items?? (wrong output) 为什么我的变量没有被访问? 以及为什么我的输出会聚集在一起而不是在新行中返回 - Why is my variable not being accessed? and why do my outputs come together instead of returning in a new line 为什么我的nslookup并不总是返回IP? - why is my nslookup not always returning an IP? 为什么我的 function 没有通过 url 中的 slug 返回 object? - Why is my function not returning the object by its slug in the url? Keras my_layer.output 返回 KerasTensor object 而不是张量 ZA8CFDE6331BD59EB62AC96F8911 - Keras my_layer.output returning KerasTensor object instead of Tensor object (in custom loss function) 为什么 function 没有返回值而不是值? (Python) - Why is function returning none instead of the value? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM