我正在尝试访问详细信息视图。以前我在url中使用了id,它运行正常。现在我的情况发生了变化,而不是id
,我想在网址中添加place_id
。为此,我使用<str:place_id>
,但它给AttributeError
。如何在url中添加place_id
并获取正确的数据。什么是slu ..我应该用它吗?但我的place_id
是独一无二的。
URLs.朋友
path('detail/<str:place_id>/', Detail.as_view(), name='detail'),
models.朋友
class Map(models.Model):
name = models.CharField(max_length=255, null=True, blank=True)
address = models.CharField(max_length=255, null=True, blank=True)
place_id = models.CharField(max_length=255, null=True, blank=True, unique=True)
views.朋友
class Detail(LoginRequiredMixin, generic.DetailView):
template_name = "detail.html"
model = Map
context_object_name = 'map'
错误 -
Generic detail view Detail must be called with either an object pk or a slug in the URLconf.
您需要告诉视图您在URL中使用place_id,并且这将映射到模型上的place_id字段,该字段应该用于查找Map。
class Detail(LoginRequiredMixin, generic.DetailView):
template_name = "detail.html"
model = Map
context_object_name = 'map'
slug_url_kwarg = 'place_id'
slug_field = 'place_id'