1
我正在嘗試爲用戶創建一個表單,以便自己更改其個人資料帳戶信息。目前我擁有它,以便他們可以更新他們的第一個,最後一個和用戶名,性別和'birthdate'。 '出生日期'是我不能不需要的唯一一個。目前,我的表單會檢查在任何表單字段中是否未輸入任何內容,如果沒有更改,則不會更新用戶信息。Django 1.4想要使用SelectDateWidget來生成我的表單中不需要的生日日期字段
~~ 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)
--Form來收集用戶的設置改變
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()時,不要求使用birthdate字段,因爲我當前的required = False嘗試不起作用。或者更好的方法來允許用戶編輯他們的設置並提交更改?
謝謝先進。
哦,我明白了。那麼會有更好的方法來保存生日變量嗎?如果我做了如下檢查: -------------------------- if request.POST ['birthdate'] =='': settingForm.birthdate = None -------------------------------------------- -------------------------- 或沒有清理的數據的東西?我用django和python還是有點新,所以處理這些東西與我的C++類稍有不同。 謝謝。 – NightCodeGT
你可以爲dictionairy get方法聲明你自己的返回類型,例如dict.get('foo',None)''只是默認的 –
我試過在django文檔中查找如何做到這一點。我不太確定是否應該在視圖或視圖調用的實際窗體中編寫自定義返回值。這似乎是聲明它的形式是正確的選擇,但我想檢查,因爲我從來沒有這樣做過。 – NightCodeGT