在 Django 1x 中,我可以在 URL 中使用两个 id 来创建 URL,如下所示:
网址.py
('^profile/(?P<cpk>[0-9]+)/env/(?P<envid>[0-9]+)/$', views.EnvironmentDetailView.as_view(), name='environment'),
HTML 模板
"{% url 'myapp:environment' cpk=environment.client.id envid=environment.id %}"
模型.py
class Environment(models.Model):
...
...
def get_absolute_url(self):
return reverse('myapp:envmanager', args =[self.client.pk])
视图.py
class EnvironmentDetailView(generic.DetailView):
'''Detail view of environment'''
model = Environment
pk_url_kwarg ="envid"
template_name = 'myapp/environment.html'
但是当我升级到 Django 2.0 时,我尝试了:
网址.py
path('profile/<int:client_id>/env/<int:pk>/', views.EnvironmentDetailView.as_view(), name='environment'),
html模板
"{% url 'myapp:environment' client_id=environment.client.id pk=environment.id %}"
视图.py
class EnvironmentDetailView(generic.DetailView):
'''Detail view of environment'''
model = Environment
pk_url_kwarg ="environment_id"
template_name = 'myapp/environment.html'
并得到了这个错误
AttributeError: Generic detail view EnvironmentDetailView must be called with either an object pk or a slug.
您如何正确引用具有两个 id 的 URL 链接?