我怎样才能在没有post_detail页面的情况下将"post id"(与评论模型连接的外键字段)获取到django评论表单
how can i get the "post id"(foreign key field connected with comment model) to the django comments form without post_detail page in django
我正在创建一个单页博客站点,我怎样才能获得评论表单的 post id,我创建了带有必要字段的 django 表单,但问题是,我必须 select post id 从下拉菜单中手动评论,为此我将 post 对象作为表单的输入值传递给 views.py 文件,但 django 需要实例来保存在数据库中我应该怎么办现在做
注意:我没有使用 post_detail
models.py
class comments(models.Model):
name=models.CharField(max_length=255)
content=models.TextField()
post=models.ForeignKey(blog,related_name="comments",on_delete=models.CASCADE)
#blog is the model to which comment is related
date=models.DateTimeField(auto_now_add=True)
forms.py
class commentform(ModelForm):
class Meta:
model=comments
fields=('name','content','post')
widgets={
'name' : forms.TextInput(attrs={'class':'form-control','placeholder':'type your name here'}),
'content' : forms.Textarea(attrs={'class':'form-control'}),
'post' : forms.Select(attrs={'class':'form-control'})
}
Html
<form method='POST' action="comment_action" class='form-group'>
{%csrf_token%}
{{form.name}}
{{form.content}}
<input type="text" id="objid" name="objid" value="{{objs.id}}" hidden>
<button class="btn btn-primary btn-sm shadow-none" type="submit">Post comment</button>
views.py
def comment_action(request):
name=request.POST.get('name')
content=request.POST.get('content')
objid=request.POST.get('objid')
to_db=comments.objects.create(name=name,content=content,post=objid)
print(name,content,objid)
return redirect('/homepage')
return render(request, 'index.html')
错误:
异常类型:ValueError
异常值:
无法分配“'48'”:“comments.post”必须是“博客”实例。
-->48是我的博客id
我知道数据库将只接受实例 post 字段,因为那是一个外键
我的问题是如何通过它?
你用post_id
分配它:
def comment_action(request):
name = request.POST.get('name')
content = request.POST.get('content')
objid = request.POST.get('objid')
to_db = comments.objects.create(name=name, content=content<b>, post_id=objid</b>)
print(name,content,objid)
return redirect('/homepage')
Note: It is better to use a Form
[Django-doc]
than to perform manual validation and cleaning of the data. A Form
will not
only simplify rendering a form in HTML, but it also makes it more convenient
to validate the input, and clean the data to a more convenient type.
Note: normally a Django model is given a singular name, so Comment
instead of comments
.
我正在创建一个单页博客站点,我怎样才能获得评论表单的 post id,我创建了带有必要字段的 django 表单,但问题是,我必须 select post id 从下拉菜单中手动评论,为此我将 post 对象作为表单的输入值传递给 views.py 文件,但 django 需要实例来保存在数据库中我应该怎么办现在做 注意:我没有使用 post_detail
models.py
class comments(models.Model):
name=models.CharField(max_length=255)
content=models.TextField()
post=models.ForeignKey(blog,related_name="comments",on_delete=models.CASCADE)
#blog is the model to which comment is related
date=models.DateTimeField(auto_now_add=True)
forms.py
class commentform(ModelForm):
class Meta:
model=comments
fields=('name','content','post')
widgets={
'name' : forms.TextInput(attrs={'class':'form-control','placeholder':'type your name here'}),
'content' : forms.Textarea(attrs={'class':'form-control'}),
'post' : forms.Select(attrs={'class':'form-control'})
}
Html
<form method='POST' action="comment_action" class='form-group'>
{%csrf_token%}
{{form.name}}
{{form.content}}
<input type="text" id="objid" name="objid" value="{{objs.id}}" hidden>
<button class="btn btn-primary btn-sm shadow-none" type="submit">Post comment</button>
views.py
def comment_action(request):
name=request.POST.get('name')
content=request.POST.get('content')
objid=request.POST.get('objid')
to_db=comments.objects.create(name=name,content=content,post=objid)
print(name,content,objid)
return redirect('/homepage')
return render(request, 'index.html')
错误:
异常类型:ValueError
异常值:
无法分配“'48'”:“comments.post”必须是“博客”实例。
-->48是我的博客id
我知道数据库将只接受实例 post 字段,因为那是一个外键 我的问题是如何通过它?
你用post_id
分配它:
def comment_action(request):
name = request.POST.get('name')
content = request.POST.get('content')
objid = request.POST.get('objid')
to_db = comments.objects.create(name=name, content=content<b>, post_id=objid</b>)
print(name,content,objid)
return redirect('/homepage')
Note: It is better to use a
Form
[Django-doc] than to perform manual validation and cleaning of the data. AForm
will not only simplify rendering a form in HTML, but it also makes it more convenient to validate the input, and clean the data to a more convenient type.
Note: normally a Django model is given a singular name, so
Comment
instead of.comments