TypeError save()至少需要2个非关键字参数(给定1个)

时间:2010-11-27 23:34:08

标签: django django-forms geoip

我有我的views.py

def status_set(request):
    ip_address= request.META['REMOTE_ADDR'] 
    if request.method == "POST":
        rform = registerForm(data = request.POST)
        if rform.is_valid():
            register = rform.save(commit=False)
            register.user=request.user
            register.save()
            return render_to_response('home.html')
    else:
        rform = registerForm() 
    return render_to_response('status_set.html',{'rform':rform}) 

并且在forms.py中有

from django.contrib.gis.utils import GeoIP


    class registerForm(forms.ModelForm): 
        class Meta:
            model=register
            fields = ('Availability', 'Status')

        def save(self,ip_address, *args, **kwargs):
            g = GeoIP()
            lat, lon = g.lat_lon('ip_address')
            user_location = super(registerForm, self).save(commit=False)
            user_location.latitude = lat
            user_location.longitude = lon
            user_location.save(*args, **kwargs)

当我试图提交表格时说

/ status-set /的TypeError save()至少需要2个非关键字参数(给定1个) 我无法找到解决方案。可能的原因是什么? 我想我必须将ip地址作为参数传递任何建议

1 个答案:

答案 0 :(得分:2)

ip_address参数是必需的,因此您必须提供它:

register.save(ip_address)

此外,您实际上似乎没有在方法中使用ip_address参数。可能你不应该在你呼叫的方法中引用ip_address

lat, lon = g.lat_lon(ip_address)