Python ValueError:字段';id';应为一个数字,但得到';favicon.ico';
我正在构建一个博客应用程序,我正在开发一个功能,突然在服务器运行时看到一个错误。如下所示↓ 在浏览器中打开某些内容时出错 一切都很好,但这是不断显示在每次点击我做 ValueError:字段“id”需要一个数字,但得到了“favicon.ico” views.pyPython ValueError:字段';id';应为一个数字,但得到';favicon.ico';,python,django,Python,Django,我正在构建一个博客应用程序,我正在开发一个功能,突然在服务器运行时看到一个错误。如下所示↓ 在浏览器中打开某些内容时出错 一切都很好,但这是不断显示在每次点击我做 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
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 )
url.py
path('<id>',views.detail_view,name='detail_view'),
按如下方式更改代码:
#url.py
路径(“”,views.detail_view,name='detail_view'),
在您的视图.py中:
def detail_视图(请求,id):
数据=获取对象或404(Post,pk=pk)
# ......
post=获取对象或404(post,pk=pk)
#......
返回重定向('detail_view',pk=pk)
希望这能解决您的问题。尝试用pk
更改id
,因为id是python中的内置函数。我更改了data=get\u object\u或_404(Post,pk
=id),但它一直显示错误。我只是注意到您正在对数据库进行额外的查询。因为数据和post变量在其中存储相同的值。我真的很感谢您的努力,但它一直显示这个错误。我还删除了另一个变量。它还显示了上述异常(以10为基数的int()的无效文本:“favicon.ico”)是以下异常的直接原因:
Ok!然后,请将您的模板文件添加到您的问题中。因为我认为问题出在你的模板上。我已经添加了模板
**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.