【发布时间】:2021-02-04 12:03:21
【问题描述】:
我正在构建一个 BlogApp,我正在开发一个功能,但突然我在服务器运行时看到一个错误。它显示如下↓当我在浏览器中打开某些东西时出错。
一切正常,但每次点击都会显示。
ValueError:字段 'id' 需要一个数字,但得到了 'favicon.ico'。
views.py
def detail_view(request,id):
data = get_object_or_404(Post,id=id)
comments = data.comments.order_by('-created_at')
new_comment = None
comment_form = CommentForm(data=request.POST)
post = get_object_or_404(Post,id=id)
if post.allow_comments == True :
if request.method == 'POST':
if comment_form.is_valid():
comment_form.instance.post_by = data
comment_form.instance.commented_by = request.user
comment_form.instance.active = True
new_comment = comment_form.save()
return redirect('detail_view',id=id)
else:
comment_form = CommentForm()
context = {'counts':data.likes.count,'post':post,'data':data,'comments':comments,'new_comment':new_comment,'comment_form':comment_form}
return render(request, 'show_more.html', context )
urls.py
path('<id>',views.detail_view,name='detail_view'),
**When i check this Error in Browser, it is showing `data = get_object_or_404(Post,id=id) ` IT MEANS that the error is in this line in Views.py.**
I don't know what's wrong in this.
Any help would be appreciated.
Thank You in Advance.
【问题讨论】:
-
尝试将
id更改为pk,因为id 是python 中的内置函数。 -
我更改了 data = get_object_or_404(Post,
pk=id) 但它一直显示错误。