我正在使用 Django 表单模块来获取价值并将其传递给用户配置文件。唯一的问题是 home_type 是“forms.ChoiceField”,但 forms.ChoiceField 没有“clean_data”类型,那么如何获取无线电选择值并将其传递给用户配置文件模块?
模块的代码是这样的:
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
productNo = models.CharField(max_length=50, blank=True)
location = models.CharField(max_length=50, blank=True)
average_temp = models.CharField(max_length=5, blank=True)
HOME_TYPE = (
('AP', 'Apartment/Condo'),
('SH', 'Single House/Residential'),
('ST', 'Studio'),
('TH', 'Townhouse'),
('MH', 'Moblie Home'),
)
home_type = models.CharField(max_length=1, choices=HOME_TYPE)
和形式的代码:
class UserProfileForm(forms.Form):
name = forms.CharField(label="Name", max_length=30, widget=forms.TextInput(attrs={'size': 20,}))
productNo = forms.CharField(label="ProductNo", max_length=50, widget=forms.TextInput(attrs={'size': 20,}))
location = forms.CharField(label="Location", max_length=50, widget=forms.TextInput(attrs={'size': 20,}))
average_temp = forms.CharField(label="Average_Temp", max_length=10, widget=forms.TextInput(attrs={'size': 20,}))
HOME_TYPE = (
('AP', 'Apartment/Condo'),
('SH', 'Single House/Residential'),
('ST', 'Studio'),
('TH', 'Townhouse'),
('MH', 'Moblie Home'),
)
home_type = forms.ChoiceField(label="home_type", widget=forms.RadioSelect(), choices=HOME_TYPE)
在我的 view.py 中,处理值传递的代码是:
def user_profile_update(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/keenhome/accounts/login/')
UserProfile = request.user.get_profile()
if request.method == 'POST':
form=UserProfileForm(request.POST)
if form.is_valid():
UserProfile.name = form.cleaned_data["name"]
UserProfile.productNo = form.cleaned_data["productNo"]
UserProfile.location = form.cleaned_data["location"]
UserProfile.average_temp = form.cleaned_data["average_temp"]
#problem happens here:
UserProfile.home_type = form.cleaned_data["home_type"]
UserProfile.save()
return HttpResponseRedirect('/keenhome/accounts/user_profile/')
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
唯一的问题是forms.ChoiceField 没有“clean_data”类型,那么我怎样才能获得无线电选择值并将其传递给用户配置文件模块?