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從URL中提取域名2. php傳對應的id值為什么傳不了啊有木有大神會的看我下方截圖3. python - scrapy url去重4. python - Flask寫的注冊頁面,當注冊時,如果填寫數據庫里有的相同數據,就報錯5. 關于mysql聯合查詢一對多的顯示結果問題6. 實現bing搜索工具urlAPI提交7. 數據庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。8. python - oslo_config9. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)10. 小白學python的問題 關于%d和%s的區別
