django 视图/模板中带有一对一字段的DoesNotExist 错误
我有这些模型。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个相关的错误: 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#L267as you see, the tag's
render
method does not catch anything butVariableDoesNotExist
.您没有提供足够的详细信息来解决此处的错误,但简单的错误消息只能由您认为的一个语句触发,即...
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...
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.