【发布时间】:2021-09-23 18:33:50
【问题描述】:
这是一个基本问题,但我无法弄清楚我做错了什么......
我正在尝试在我的 django 站点中呈现详细视图,但我获取对象的调用失败或未呈现。
这是我所拥有的: 视图.py
from django.template import loader
from django.views.generic.base import TemplateView
from django.http import HttpResponse
from .models import user_profile
def detail(request, content_id):
template = loader.get_template('profiles/detail.html')
profile = user_profile.objects.get(pk=content_id)
context = {'profile': profile}
return HttpResponse(template.render(context, request))
在模板中,我只是使用以下方法测试调用:
<h1>{{ profile }}</h1>
<h1>{{ profile.name }}</h1>
这是我第一次从头开始渲染,我知道我错过了一些愚蠢的东西,我无法对其进行排序。谢谢!
edit 此当前设置收到 500 错误。如果没有 get(pk) 语句,它会加载,但不显示变量,只显示我的 HTML 的其余部分。
根据要求,这里是 urls.py:
urlpatterns=[
path('<int:content_id>/', views.detail, name='detail'),
]
编辑:解决方案已解决!最终导致模型出现问题,即未正确执行迁移并且列被读取为缺失。
【问题讨论】:
-
显示你的 urls.py
-
请将您的
settings.DEBUG设置为True并将错误回溯添加到您的问题中(在错误页面上有一个按钮可以显示可复制粘贴的回溯) -
如果配置文件不存在您的“content_id”,则可能会发生错误
-
由于安全协议,debug 设置为 true,但不幸的是,进一步的日志记录被阻止......我已经通过直接查看 postgres 数据库确认 content_id 存在......
标签: django django-models django-views django-templates