尝试自动填充 django 表单中的表单字段

发布于 2024-10-07 05:38:26 字数 1338 浏览 3 评论 0原文

我正在尝试自动填充表单中的纬度和经度字段 我正在以这种方式做这件事

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

            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)

,我的视图有这个东西

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

,但是当我尝试提交表单时,它说“save() 需要至少 2 个非关键字参数(给定 1 个)”,错误在这里“ register = rform.save(commit=False)" 我无法弄清楚 使用 django 1.1.1 和 GeoIP 的任何帮助将不胜感激

i am trying to auto populate the latitude and longitude field in my forms
I am doing this thing in this way

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

            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)

and my views have this thing

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

but when i try to submit the forms it says "save() takes at least 2 non-keyword arguments (1 given)"the error is here " register = rform.save(commit=False)" i em unable to figure it out
any help will be greatly appreciated em using django 1.1.1 and GeoIP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-10-14 05:38:26

查看 registerForm 上的 save 定义 - 您添加了 ipaddress 作为参数,但在您看来,您没有将其传入如果您想继续按现在的方式调用 save(),请在函数定义中设置 ipaddress=None 或您可以处理的其他默认值。

Take a look at your save definition on your registerForm - you added ipaddress as an argument, but in your view you aren't passing it in. If you want to keep calling save() the way you are now, make ipaddress=None in your function definition, or some other default value that you can handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文