为什么我的表单在Django中成功保存,除了一个字段? [英] Why does my form in Django save successfully except for one field?
问题描述
我的表单传递给我的views.py中的该方法,如果我进入python shell并从MyProfile打印对象,则所有字段均显示值,但附近的zips(无)显示。正如您在下面看到的那样,保存表单时,我试图为附近的zip手动分配一个值。
my form in forms.py is then passed to this method in my views.py, if I go into python shell and print objects from MyProfile, all of the fields show values except for nearbyzips, which shows None. As you can see below, I am trying to manually assign a value to nearbyzips when the form is saved.
inside views.py
inside views.py
@secure_required
@login_required
def profile_edit(request, username, edit_profile_form=EditProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None, **kwargs):
profile = get_profile(user)
form = edit_profile_form(instance=profile, initial=user_initial)
if request.method == 'POST':
if form.is_valid()
cleanzipcode = form.cleaned_data['zipcode']
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)
zip_codes = list(nearestzips.values_list('code', flat=True))
//print zip_codes
form.cleaned_data['nearbyzips'] = zip_codes
//print form.cleaned_data['nearbyzips']
profile=form.save()
return redirect(redirect_to)
models.py
models.py
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,
unique=True,
verbose_name=_('user'),
related_name='my_profile')
streetaddress=models.CharField(null=True, blank=True, max_length=30)
city = models.CharField(null=True, blank=True, max_length=20)
state = models.CharField(null=True, blank=True, max_length=20)
zipcode = models.IntegerField(_('zipcode'),
max_length=5, null=True, blank=True)
nearbyzips = models.IntegerField(null=True, blank=True, max_length=100)
phone=models.CharField(null=True, blank=True, max_length=16)
websiteurl=models.CharField(null=True, blank=True, max_length=38)
要记住的一点是,如果我进入python shell并运行:
Something to keep in mind, if I go into python shell and run:
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='97202').location
print nearestzips
它会打印我期望的所有邮政编码。所以我不确定到底是哪里坏了。我的日志中没有看到任何错误。
It prints all the Postal Codes I would expect. So I'm not sure where exactly is broken. I don't see any errors in my logs.
更新:
我在视图中添加了打印语句。打印邮政编码和form.cleaned_data ['nearbyzips']都显示:
UPDATE: I have added print statements in my views. printing zip_codes and form.cleaned_data['nearbyzips'] both show:
[u'97202', u'97206', u'97214', u'97215', u'97239']
但它似乎仍然没有
推荐答案
更改此行:
tzips = PostalCode.objects.distance(PostalCode.objects.get(code='cleanzipcode').location)
为此:
tzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)
这篇关于为什么我的表单在Django中成功保存,除了一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!