尝试自动填充 django 表单中的表单字段
我正在尝试自动填充表单中的纬度和经度字段 我正在以这种方式做这件事
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
registerForm
上的save
定义 - 您添加了ipaddress
作为参数,但在您看来,您没有将其传入如果您想继续按现在的方式调用save()
,请在函数定义中设置ipaddress=None
或您可以处理的其他默认值。Take a look at your
save
definition on yourregisterForm
- you addedipaddress
as an argument, but in your view you aren't passing it in. If you want to keep callingsave()
the way you are now, makeipaddress=None
in your function definition, or some other default value that you can handle.