Django 1.4想要使用我的表单上不需要的SelectDateWidget来制作生日字段

时间:2012-06-04 20:38:07

标签: django django-models django-forms django-views django-widget

我正在尝试为用户创建一个表单,以便他们自己更改其个人资料帐户信息。目前我有它,所以他们可以更新他们的第一个,最后一个和用户名,性别和'生日'。 'birthdate'是我唯一不能被要求的。目前,我的表单检查在任何表单字段中是否未给出任何输入,如果未进行任何更改,则不会更新用户信息。

~~ Models.py ~~

class Account(models.Model):
    user               = models.OneToOneField(User)      #link (pointer) to the users     other information in User model                                        
    birthdate          = models.DateField(blank = True, null = True) # True makes this field optional                                                        
    gender             = models.CharField(max_length = 10, choices = GENDER_CHOICE, null = True, blank = True)
    profilePic         = models.ImageField(upload_to = "profilepics/%Y/%m/%d", default = "profilepics/default.jpg", blank = True)

- 用于收集用户设置更改的表单

class EditAccountForm(forms.Form):
    username           = forms.CharField(max_length = 30, required = False)
    first_name         = forms.CharField(max_length = 30, required = False)
    last_name          = forms.CharField(max_length = 30, required = False)
    birthdate          = forms.DateField(widget = SelectDateWidget(required = False, years = range(2022, 1930, -1))) # make birthdate drop down selectable    
    gender             = forms.CharField(max_length = 10, widget=forms.Select(choices = GENDER_CHOICE),)# null = True)                 

~~ Views.py

def AccountSettings(request):
    sluggedSettingError = request.GET.get('error', '') # error message with slugged character                                                                
    settingError = sluggedSettingError.replace('-', ' ')
    # form variable may need to be inside below if statement                                                                                                 
    settingForm = EditAccountForm(request.POST or None) # variable is called in edit_user.html                                                               
    if request.method == 'POST':
        if settingForm.is_valid():

            userSettings = request.user.get_profile() # returns the current settings of the users profile 
if request.POST['gender'] == '---':
                print "Gender was not changed"
            else:
                userSettings.gender = request.POST['gender']  # add a check for birthdate submission                                                         
            userSettings.birthdate = settingForm.cleaned_data.get('birthdate')   # currently requires input format YYYY-mm-dd                                
            # check if the first name submission is not changed                                                                                              
            if request.POST['first_name'] == '':
                print "First name not changed"
            else:
                userSettings.user.first_name = request.POST['first_name']
            # check if the last name submission is not changed                                                                                               
            if request.POST['last_name'] == '':
                print "Last name not changed"
            else:
                userSettings.user.last_name = request.POST['last_name']
            # check if the username submission is not changed                                                                                                
            if request.POST['username'] == '':
                print "Username not changed"
            else:
                userSettings.user.username = request.POST['username']
            # check if the ProfilePicture submission is not changed                                                                                          
#                if request.POST['profilePic'] == None:
#                    print "Profile picture not changed"
#                else:
#                    userSettings.profilePic = request.POST['profilePic']
            userSettings.user.save()              # save the changes to the fields in used from the auth library                                             
            userSettings.save()                   # save the altered and unaltered settings                                                                  
            return HttpResponseRedirect('/')
    return render_to_response("user_settings.html", {'settingForm': settingForm, 'settingError': settingError}, context_instance = RequestContext(request))

有没有办法在使用SelectDateWidget()时不需要生日字段,因为我当前尝试的required = False不起作用。或者可能是一种更好的方法,允许用户编辑他们的设置并提交更改?

先谢谢。

1 个答案:

答案 0 :(得分:2)

我希望你的问题源于:

settingForm.cleaned_data.get('birthdate')

不会返回None,而是递给你一个空字符串'',这不是Date或DateTime字段所期望的。

如果用户实际上没有选择日期,则应返回NoneType。

相关问题