python - Django表單Form.save()問題
問題描述
def commentCreate(request, articleId): #POST’’’the problem is comment no relate mapping to one id of Artilce 單純的將CommentForm().save 是無法實(shí)現(xiàn)留言的,因?yàn)? 創(chuàng)建一個(gè)留言需要對應(yīng)一個(gè)文章,即要對應(yīng)其文章所屬id;’’’content = request.POST.get(’comment’)CommentForm.content = contentcomment = CommentForm() #print (’表單綁定確認(rèn):’,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).網(wǎng)頁錯誤: comment.save() ...▼ Local varsVariable ValuearticleId ’336’comment <CommentForm bound=False, valid=False, fields=(content)>content ’你好’request <WSGIRequest: POST ’/article/commentCreate/336/’>
如何將文章對應(yīng)的articleId 的值放進(jìn)CommentForm()?
問題解答
回答1:根據(jù)articleId獲取到Article: obj = Article.objects.get(id=articleId)然后 comment.article = obj # 具體看你在models里定義的article字段名
回答2:嗯,謝謝你,這個(gè)問題我在http://zmrenwu.com/post/14/ 里面已經(jīng)找到解決方法了。不過這個(gè)論壇怎么把提問刪掉。
相關(guān)文章:
1. javascript - node.js promise沒用2. golang - 用IDE看docker源碼時(shí)的小問題3. yii2中restful配置好后在nginx下報(bào)404錯誤4. 算法 - python 給定一個(gè)正整數(shù)a和一個(gè)包含任意個(gè)正整數(shù)的 列表 b,求所有<=a 的加法組合5. android 如何實(shí)現(xiàn)如圖中的鍵盤上的公式及edittext的內(nèi)容展示呢6. java - 我在用Struts2上傳文件時(shí),報(bào)以下錯誤怎么回事?7. c++ - 如何正確的使用QWebEngineView?8. PHP注冊功能9. mysql - 求SQL語句10. MySQL如何實(shí)現(xiàn)表中再嵌套一個(gè)表?
