django 视图/模板中带有一对一字段的DoesNotExist 错误

发布于 2024-12-07 22:04:08 字数 961 浏览 0 评论 0原文

我有这些模型。

class Storypak(models.Model):
    headline = models.CharField('Headline', max_length=200)
    pub_date = models.DateField(blank=True)

class Story(models.Model):
    storypak = models.OneToOneField('Storypak', blank=True, null=True)
    copy = models.TextField(blank=True)

还有这个观点。

def pak_detail(request, pak_id, year, month, day):
    pak = Storypak.objects.get(pk=pak_id)
    t = loader.get_template('storypak_detail.html')
    c = Context ({
        'pak' : pak,
        })
    return HttpResponse(t.render(c))

当我尝试在模板中使用 if 语句时,会引发 DoesNotExist 错误。我能找到的文档表明这些错误应该被消除。 if pak.story 不应该解析 False 并且不抛出错误吗?我缺少什么?我认为这可能与 OneToOne 关系有关,但我在文档中找不到专门处理此问题的任何内容。

这是我记得的相关模板代码。我这台计算机上没有该文件。如果它不正确,我稍后会修复它,如果有帮助的话,我可能会发布调试信息。

{% if pak.story %}
    <p>{{ pak.story.copy }}</p>
{% endif %}

I have these models.

class Storypak(models.Model):
    headline = models.CharField('Headline', max_length=200)
    pub_date = models.DateField(blank=True)

class Story(models.Model):
    storypak = models.OneToOneField('Storypak', blank=True, null=True)
    copy = models.TextField(blank=True)

And this view.

def pak_detail(request, pak_id, year, month, day):
    pak = Storypak.objects.get(pk=pak_id)
    t = loader.get_template('storypak_detail.html')
    c = Context ({
        'pak' : pak,
        })
    return HttpResponse(t.render(c))

When I try to use an if statement in my template, a DoesNotExist error is thrown. The documentation I can find indicates that these errors should be silenced. Shouldn't if pak.story resolve False and not throw an error? What am I missing? I think it may have something to do with the OneToOne relationship, but I can't find anything in the docs dealing with this specifically.

Here is the relevant template code as I remember it. I don't have the file on this computer. I'll fix it later if it isn't correct and possibly post the debug info if that would help.

{% if pak.story %}
    <p>{{ pak.story.copy }}</p>
{% endif %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

铜锣湾横着走 2024-12-14 22:04:08

这是一个相关的错误: https://code.djangoproject.com/ticket/10227

这里是if 标签的来源:https://code.djangoproject.com/browser/django/trunk /django/template/defaulttags.py#L267

如您所见,标签的 render 方法不会捕获任何内容,但变量不存在

here is a related bug: https://code.djangoproject.com/ticket/10227

here is the source for the if tag: https://code.djangoproject.com/browser/django/trunk/django/template/defaulttags.py#L267

as you see, the tag's render method does not catch anything but VariableDoesNotExist.

伪心 2024-12-14 22:04:08

您没有提供足够的详细信息来解决此处的错误,但简单的错误消息只能由您认为的一个语句触发,即...

pak = Storypak.objects.get(pk=pak_id)

pak_id 无效,或者您的模型格式错误并且是 Storypak该 id 不存在。这是唯一会引发DoesNotExist 错误的对象调用。您可以通过在此行之前添加 raise Exception(str(pak_id)) 来验证它是否是有效的 ID,以查看它试图“获取”的内容。验证 Storypak 表中是否存在该记录。

You haven't given enough detail to troubleshoot much past the error here, but the simple error message can only be triggered by one statement in your view, that is...

pak = Storypak.objects.get(pk=pak_id)

The pak_id is either invalid, or your model is malformed and a Storypak with that id doesn't exist. That is the only call to an object that would raise a DoesNotExist error. You can verify it is a valid id by adding raise Exception(str(pak_id)) before this line to see what it is attempting to "get." Verify that record exists in the storypak table.

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