我在Django項目中有一個表單,並且我希望用戶輸入他的生日,以便我可以保存在數據庫中並在以後的項目中使用...試圖讓用戶選擇生日 - django
我有一個模型,我想創建一個Modelform,它會詢問日期,然後格式化並保存在數據庫中。我一直在試圖找出這3天,並無法得到它的工作......我不得不刪除我的數據庫文件大約5次,因爲它不工作,搞亂了數據庫...
當用戶提交表單時,回報是三個單獨的項目,其中包括dob_month,dob_day,dob_year和正確的日期信息。
這是我到目前爲止所做的。
models.py文件
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) # server
first_name = models.CharField(max_length=25, default='first')
last_name = models.CharField(max_length=25, default='last')
dob = models.DateField(auto_now_add=False, blank=True)
city = models.CharField(max_length=45) # user
state = models.CharField(max_length=25, default='state')
phone = models.BigIntegerField(default=0) # user
privacy = models.SmallIntegerField(default=1) # user
created = models.DateTimeField(auto_now_add=True) # server
這裏是forms.py文件:
class ProfileForm(forms.ModelForm):
split_choices = (('1', 'public'),
('2', 'private'))
privacy = forms.TypedChoiceField(
choices=split_choices, widget=forms.RadioSelect, coerce=int
)
dob = forms.DateField(widget=extras.SelectDateWidget)
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'dob', 'city', 'state', 'phone', 'privacy']
這裏是view.py文件(處理表單的具體DEF ...)
def profile_setup(request):
if 'username' not in request.session:
return redirect('login')
else:
# the following is just going to grab the currently logged in user and
# save the profile information to the appropriate user
username = request.session['username']
currentUser = User.objects.get(username = username)
# the following is the provessing for the form where the user entered
# the profile informaiton
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
first_name = cd['first_name']
last_name = cd['last_name']
dob_month = form.cleaned_data.get("dob_month")
dob_day = form.cleaned_data.get("dob_day")
dob_year = form.cleaned_data.get("dob_year")
city = cd['city']
state = cd['state']
phone = cd['phone']
privacy = cd['privacy']
# this is the new record that is going to be created and saved
new_profile = Profile.objects.create(
user = currentUser,
first_name = first_name,
last_name = last_name,
dob_month = dob_month,
dob_day = dob_day,
dob_year = dob_year,
city = city,
state = state,
phone = phone,
privacy = privacy,
)
return redirect('home_page')
else:
# this is what is going to be saved into the html file and used to
# render the file
form = ProfileForm()
message = 'fill out form below'
parameters = {
'form':form,
'currentUser':currentUser,
'message':message,
}
return render(request, 'tabs/profile_setup.html', parameters)
**更新第**
所以可以說我有類似下面的模式:
class Group(models.Model):
name = models.CharField(max_length = 25)
description = models.CharField(max_length = 250, null=True)
count = models.SmallIntegerField(default=1)
status = models.SmallIntegerField(choices=GROUP_STATUS_CHOICES, default=1)
reference_code = models.IntegerField(default=0)
安迪有類似下面的模型形式:
class CreateGroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ['name', 'count', 'description']
,並在用戶提交以下形式:
name : omar jandali
count : 5
description : random group
那麼我可以通過以下方式處理表格:
form = CreateGroupForm(request.POST, instance=new_group)
new_group = Group(
name = name,
description = desciption,
count = count,
status = status,
)
或者我還需要分配清理過的數據......
因此,對於任何模型形式,我可以創建它的一個實例,並保存它,而不必像現在正在處理它一樣處理它....你能看看我的文章的更新部分,告訴我我可以做像我的任何形式的東西[email protected] –
因爲您正在使用帶有名稱,描述和計數字段的模型表單,所以不需要爲new_group添加名稱,描述和計數。您只需要status和reference_code的值。還要確保你的表單標籤在你的new_group之前沒有或者它不起作用。 – jonin