国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python - Django表單Form.save()問題

瀏覽:118日期:2022-06-30 14:02:21

問題描述

def commentCreate(request, articleId): #POST’’’the problem is comment no relate mapping to one id of Artilce 單純的將CommentForm().save 是無法實現(xiàn)留言的,因為 創(chuàng)建一個留言需要對應(yīng)一個文章,即要對應(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:

嗯,謝謝你,這個問題我在http://zmrenwu.com/post/14/ 里面已經(jīng)找到解決方法了。不過這個論壇怎么把提問刪掉。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 武功县| 南召县| 沙坪坝区| 长子县| 邻水| 醴陵市| 云霄县| 全州县| 如东县| 二连浩特市| 吴桥县| 甘谷县| 赤壁市| 芷江| 盖州市| 界首市| 永平县| 镇沅| 海盐县| 湾仔区| 曲靖市| 交口县| 饶阳县| 巩义市| 宜州市| 襄樊市| 象山县| 镇巴县| 洛南县| 甘孜| 景泰县| 万荣县| 城步| 新竹县| 富阳市| 新宁县| 乐昌市| 巴里| 临沭县| 烟台市| 四会市|