django错误(外部IP)
每次输入不存在的地址时我都会收到通知。
回溯(最近一次调用最后一次): 文件“/home/user/opt/local/django/core/handlers/base.py”,第 100 行,在 get_response 中 文件“/web/blog/views.py”,第 33 行,在帖子中 文件“/home/user/local/django/db/models/manager.py”,第 132 行,在 get 中 文件“/home/user/opt/local/django/db/models/query.py”,第 347 行,在 get 中 doesNotExist:发布匹配查询不存在。
如何解决
I am receiving notifications every time, when enter an address that does not exist.
Traceback (most recent call last):
File "/home/user/opt/local/django/core/handlers/base.py", line 100, in get_response
File "/web/blog/views.py", line 33, in post
File "/home/user/local/django/db/models/manager.py", line 132, in get
File "/home/user/opt/local/django/db/models/query.py", line 347, in get
DoesNotExist: Post matching query does not exist.
how to solve it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
修改您的查询以使用
get_object_or_404< /code>
,或者捕获
定义了它YourModel。进行查找时出现DoesNotExist
(第3段)异常,并引发Http404
异常。当您没有捕获DoesNotExist
异常时,视图会引发 500 错误。作为副作用,这会向 < 发送异常电子邮件code>ADMINSsettings.py
。两种情况的示例:
Modify your query to use
get_object_or_404
, or catch theYourModel.DoesNotExist
(3rd paragraph) exception when you're doing the lookup, and raise aHttp404
exception. When you don't catch theDoesNotExist
exception the view raises a 500 error. As a side effect, this sends an exception email to the theADMINS
defined itsettings.py
.Example of both cases:
生成错误的原因是您的
get
查询未能匹配任何记录。如果您想在此类事件中抛出404
页面,则 sdolan 已经向您提供了如何做到这一点的建议。但是,如果您希望在查询无法获取任何匹配记录的情况下采用一些合理的默认值,则可以将对get
的调用包装在try
和 <代码>catch 块。例如:The error was generated because your
get
query had failed to match any record. If you want to throw a404
page in such an event, then sdolan has already provided with you advice on how to do that. However, if you would like to assume some sensible defaults in the event that the query fails to fetch any matching records, you could wrap the call toget
around atry
andcatch
block. For example:在查询中使用 get_object_or_404,请参阅文档以获取更多信息。
use get_object_or_404 in query, see de documentation for more information.
我想主要问题出在您的视图文件中,您正在使用http响应请求对象。检查settings.py中的所有设置是否准确,还可以使用try except块来更准确地查找错误。
I guess the main problem is in your view file,where you are working with http response request object .Check that all the settings are accurate in settings.py,also use try except block for finding the error more precisely.