python - Django表单Form.save()问题
问题描述
def commentCreate(request, articleId): #POST’’’the problem is comment no relate mapping to one id of Artilce 单纯的将CommentForm().save 是无法实现留言的,因为 创建一个留言需要对应一个文章,即要对应其文章所属id;’’’content = request.POST.get(’comment’)CommentForm.content = contentcomment = CommentForm() #print (’表单绑定确认:’,comment.is_bound)’’’if not comment.is_valid(): return render(request, template,{’article’:articleId})’’’comment.save()messages.success(request,’留言已新增’)return redirect(’article:articleRead’,articleId=articleId)Console错误:django.db.utils.IntegrityError: 错误: 在字段 'article_id' 中空值违反了非空约束DETAIL: 失败, 行包含(879, , null, 2017-05-22 11:05:06.862614+00, null).网页错误: comment.save() ...▼ Local varsVariable ValuearticleId ’336’comment <CommentForm bound=False, valid=False, fields=(content)>content ’你好’request <WSGIRequest: POST ’/article/commentCreate/336/’>
如何将文章对应的articleId 的值放进CommentForm()?
问题解答
回答1:根据articleId获取到Article: obj = Article.objects.get(id=articleId)然后 comment.article = obj # 具体看你在models里定义的article字段名
回答2:嗯,谢谢你,这个问题我在http://zmrenwu.com/post/14/ 里面已经找到解决方法了。不过这个论坛怎么把提问删掉。
相关文章:
1. python for循环中的函数只能运行一次?2. 怎样写一个时间到了或是有订单的语音提醒呢?3. javascript - js读取excel其中一列中的一个值是0.3556但是读出来是0.35559999999999997?4. angular.js - angular ui bootstrap 中文显示问题5. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?6. html5 - h5页面在iphone上字体变形7. javascript - 关于apply()与call()的问题8. html - 竖线怎么实现9. mysql - SELECT 多個資料表及多個欄位10. web服务器 - ubuntu下布置apache加wsgi加python

网公网安备