我有一个简单的表单,允许用户创建帖子。这些字段是post_title, post_content, post_date. 和将由用户提供,但 和将由系统自动生成,并给出当前时间戳post_title。因此,我只会在表格中显示和。然而,当我尝试提交请求时,这会给我一个 IntegrityError 提示。我对 Django 相当陌生,所以我真的不知道该怎么做。我希望每当用户发送 Post 请求(并且如果它通过验证)时,它都会在将其保存到数据库之前生成当前时间戳。对此有何建议?多谢!post_contentpost_datetitlecontentNOT NULL constraint failed: main_app_post.post_datepost_date
models.py:
class Post(models.Model):
post_title = models.CharField(max_length=100)
post_content = models.TextField(max_length=400)
post_date = models.DateTimeField()
def __str__(self):
return self.post_title
forms.py:
class PostForm(forms.ModelForm):
class Meta:
model = models.Post
fields = ('post_title', 'post_content')
views.py:
if request.method == 'POST':
form = forms.PostForm(request.POST)
if form.is_valid():
post_title = form.cleaned_data['post_title']
post_content = form.cleaned_data['post_content']
post_date = datetime.datetime.now().timestamp()
form.post_date = post_date
form.save(commit=True)
吃鸡游戏
慕婉清6462132
相关分类