我无法将日期存储到数据库中。当我使用debug时,有:
cleaned data = {'nick_name': 'Leren', 'gender': 'male', 'birday': None, 'address': '617 bridge', 'mobile': '2263408858'}
data = <QueryDict: {'nick_name': ['Leren'], 'birthday': ['2018-04-04'], 'gender': ['male'], 'address': ['617 bridge'], 'mobile': ['2263408858'], 'email': ['1@qq.com'], 'csrfmiddlewaretoken': ['1bzcpBzTOra7yTFBiwXc9sPLw8V6ETf7ciCDNqt1Jo9EtXZ4Q1qVBCIEWVVwPxGi']}>
我不知道为什么清洁数据中不存在小鸟,但它存在于数据中。 我怎样才能将它存储到我的数据库中?
html文件:
<form class="perinform" id="jsEditUserForm" autocomplete="off">
<ul class="right">
<li>nickname:
<input type="text" name="nick_name" id="nick_name" value="{{ request.user.nick_name|default_if_none:'' }}" maxlength="10">
<i class="error-tips"></i>
</li>
<li>birthday:
<input type="text" id="birth_day" name="birthday" value="{{ request.user.birday|date:'Y-m-d H:i:s' }}" />
<i class="error-tips"></i>
</li>
<li>gender:
<label> <input type="radio" name="gender" value="male" {% ifequal request.user.gender "male" %}checked="checked"{% endifequal %} >male</label>
<label> <input type="radio" name="gender" value="female" {% ifequal request.user.gender "female" %}checked="checked"{% endifequal %}>female</label>
</li>
<li class="p_infor_city">address:
<input type="text" name="address" id="address" placeholder="" value="{{ request.user.address|default_if_none:'' }}" maxlength="10">
<i class="error-tips"></i>
</li>
<li>phone <input type="text" name="mobile" id="mobile" placeholder="" value="{{ request.user.mobile|default_if_none:'' }}" maxlength="10">
</li>
<li>email:
<input class="borderno" type="text" name="email" readonly="readonly" value="{{ request.user.email }}"/>
<span class="green changeemai_btn">[edit]</span>
</li>
<li class="button heibtn">
<input type="button" id="jsEditUserBtn" value="save">
</li>
</ul>
{% csrf_token %}
</form>
views.py:
class UserInfoView(LoginRequiredMixin,View): #用户个人信息 所以必须登陆才可以访问
def get(self,request):
return render(request,"usercenter-info.html",{
})
def post(self, request):
# 不像用户咨询是一个新的。需要指明instance。不然无法修改,而是新增用户
user_info_form = UserInfoForm(request.POST, instance=request.user)
if user_info_form.is_valid():
user_info_form.save()
return HttpResponse(
'{"status":"success"}',
content_type='application/json')
else:
# 通过json的dumps方法把字典转换为json字符串
return HttpResponse(
json.dumps(
user_info_form.errors),
content_type='application/json')
models.py
class UserProfile(AbstractUser):
nick_name=models.CharField(max_length=50,verbose_name="nickname",default="")
birday=models.DateField(verbose_name="birthday",null=True,blank=True)
gender=models.CharField(choices=(("male","male"),("female","female")),default="",max_length=10)
address=models.CharField(max_length=100,default="")
mobile=models.CharField(max_length=11,null=True,blank=True)
image=models.ImageField(upload_to="image/%Y/%m",default="image/default.png",max_length=100)
email = models.CharField(max_length=50,verbose_name="email",default="")
class Meta:
verbose_name="user profile"
verbose_name_plural=verbose_name
def __str__(self):
return self.username
forms.py:
class UserInfoForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['nick_name','gender','birday','address','mobile']
上面有我的代码。 将数据转换为清理数据时可能会出现问题