1

我正在使用 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”类型,那么我怎样才能获得无线电选择值并将其传递给用户配置文件模块?

4

3 回答 3

2

您不需要再次编写表单的所有字段。这就是它被称为 ModelForm 的原因,它应该是从模型自动生成的:

HOME_TYPE = (
    ('AP', 'Apartment/Condo'),
    ('SH', 'Single House/Residential'),
    ('ST', 'Studio'),
    ('TH', 'Townhouse'),
    ('MH', 'Moblie Home'),
)

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 = models.CharField(max_length=2, choices=HOME_TYPE)

表格.py:

class UserProfileForm(ModelForm):
   class Meta:
       model = UserProfile
       exclude = (user,)

视图.py:

def user_profile_update(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/keenhome/accounts/login/')
    form = UserProfileForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.user = request.user
            form.save()
            return HttpResponseRedirect('/keenhome/accounts/user_profile/')
    return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))

编辑:使 max_length = 2,感谢@ndpu,排除用户,添加用户保存形式,感谢@void

于 2013-11-07T20:24:27.993 回答
1
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    .....


class UserProfileForm(ModelForm):
    pass
    class Meta:
        model = UserProfile
        exclude = (user,) # you need to exclude user

现在所有这些代码......:

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))

会变成:

def user_profile_update(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/keenhome/accounts/login/')

    form = UserProfileForm(request.POST or None)

    if form.is_valid():
        form = form.save(commit=False) 
        form.user = request.user# because user is excluded
        form.save()
        #code
    else:
        #code

    return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
于 2013-11-07T20:29:27.460 回答
1

我认为问题出在这里:

class UserProfile(models.Model):
    #...
    home_type = models.CharField(max_length=1, choices=HOME_TYPE)

应该是max_length=2

    home_type = models.CharField(max_length=2, choices=HOME_TYPE)
于 2013-11-07T20:34:46.340 回答