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 = content
comment = 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 vars
Variable Value
articleId
'336'
comment
<CommentForm bound=False, valid=False, fields=(content)>
content
'你好'
request
<WSGIRequest: POST '/article/commentCreate/336/'>
Bagaimana untuk meletakkan nilai articleId yang sepadan dengan artikel ke dalam CommentForm()?
小伙看你根骨奇佳,潛力無限,來學(xué)PHP伐。
Dapatkan Artikel berdasarkan articleId: obj = Article.objects.get(id=articleId)
Kemudian komen.article = obj # Bergantung pada nama medan artikel yang anda takrifkan dalam model
Baiklah, terima kasih, saya telah menemui penyelesaian untuk masalah ini di http://zmrenwu.com/post/14/. Tetapi bagaimana saya boleh memadamkan soalan dari forum ini?