django错误(外部IP)

发布于 2024-11-09 00:02:16 字数 380 浏览 0 评论 0原文

每次输入不存在的地址时我都会收到通知。

回溯(最近一次调用最后一次): 文件“/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

层林尽染 2024-11-16 00:02:16

修改您的查询以使用 get_object_or_404< /code>,或者捕获 YourModel。进行查找时出现DoesNotExist(第3段)异常,并引发Http404 异常。当您没有捕获 DoesNotExist 异常时,视图会引发 500 错误。作为副作用,这会向 < 发送异常电子邮件code>ADMINS 定义了它 settings.py

两种情况的示例:

from django.shortcuts import get_object_or_404

post_id = 1
post = get_object_or_404(Post, id=post_id)

# or catch the exception and do something with it

from django.http import Http404
try:
    post = Post.objects.get(id=post_id)
except Post.DoesNotExist:
    # id doesnt exist... do extra things here
    raise Http404

Modify your query to use get_object_or_404, or catch the YourModel.DoesNotExist (3rd paragraph) exception when you're doing the lookup, and raise a Http404 exception. When you don't catch the DoesNotExist exception the view raises a 500 error. As a side effect, this sends an exception email to the the ADMINS defined it settings.py.

Example of both cases:

from django.shortcuts import get_object_or_404

post_id = 1
post = get_object_or_404(Post, id=post_id)

# or catch the exception and do something with it

from django.http import Http404
try:
    post = Post.objects.get(id=post_id)
except Post.DoesNotExist:
    # id doesnt exist... do extra things here
    raise Http404
许仙没带伞 2024-11-16 00:02:16

生成错误的原因是您的 get 查询未能匹配任何记录。如果您想在此类事件中抛出 404 页面,则 sdolan 已经向您提供了如何做到这一点的建议。但是,如果您希望在查询无法获取任何匹配记录的情况下采用一些合理的默认值,则可以将对 get 的调用包装在 try 和 <代码>catch 块。例如:

try:
    post = Post.object.get(pk=id)
except Post.DoesNotExist:
    post = None
    # Probably use some sensible defaults, or do something else

The error was generated because your get query had failed to match any record. If you want to throw a 404 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 to get around a try and catch block. For example:

try:
    post = Post.object.get(pk=id)
except Post.DoesNotExist:
    post = None
    # Probably use some sensible defaults, or do something else
二货你真萌 2024-11-16 00:02:16

在查询中使用 get_object_or_404,请参阅文档以获取更多信息。

http://docs.djangoproject .com/en/1.3/topics/http/shortcuts/#get-object-or-404

use get_object_or_404 in query, see de documentation for more information.

http://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#get-object-or-404

遗心遗梦遗幸福 2024-11-16 00:02:16

我想主要问题出在您的视图文件中,您正在使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文