Python 必须使用URLconf中的对象pk或slug调用通用详细信息视图
Django在我尝试访问Python 必须使用URLconf中的对象pk或slug调用通用详细信息视图,python,django,django-views,Python,Django,Django Views,Django在我尝试访问/domains/{domain}/{host}时给了我以下错误: Generic detail view HostDetailPageView must be called with either an object pk or a slug in the URLconf. 看来,DetailView期望的与我提供的不同 url.py urlpatterns = [ path('domains/', DomainsPageView.as_view()),
/domains/{domain}/{host}
时给了我以下错误:
Generic detail view HostDetailPageView must be called with either an object pk or a slug in the URLconf.
看来,DetailView
期望的与我提供的不同
url.py
urlpatterns = [
path('domains/', DomainsPageView.as_view()),
path('domains/<str:domain>', DomainHostsPageView.as_view()),
path('domains/<str:domain>/<str:host>', HostDetailPageView.as_view()),
path('', TemplateView.as_view(template_name="hosts/index.html"))
]
class HostDetailPageView(DetailView):
template_name = 'hosts/hostdetail.html'
model = Host
# Populates list of enabled domains in the context
def get_queryset(self):
qs = super().get_queryset()
filtered = qs.filter(name=self.kwargs['host'])
if not filtered.exists():
raise Http404("Host does not exist")
# filter by a variable captured from url, for example
return filtered.first()
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs)
class Host(models.Model):
created = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
name = models.CharField(unique=True, max_length=settings.MAX_CHAR_COUNT)
ip_addresses = models.ManyToManyField(IPAddress)
services = models.ManyToManyField(Service)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
os = models.ForeignKey(OperatingSystem, on_delete=models.CASCADE, blank=True, null=True)
ciphers = models.ManyToManyField(Cipher, blank=True)
certificate = models.ForeignKey(Certificate, on_delete=models.CASCADE, blank=True, null=True)
class Meta:
ordering = ['name']
型号.py
urlpatterns = [
path('domains/', DomainsPageView.as_view()),
path('domains/<str:domain>', DomainHostsPageView.as_view()),
path('domains/<str:domain>/<str:host>', HostDetailPageView.as_view()),
path('', TemplateView.as_view(template_name="hosts/index.html"))
]
class HostDetailPageView(DetailView):
template_name = 'hosts/hostdetail.html'
model = Host
# Populates list of enabled domains in the context
def get_queryset(self):
qs = super().get_queryset()
filtered = qs.filter(name=self.kwargs['host'])
if not filtered.exists():
raise Http404("Host does not exist")
# filter by a variable captured from url, for example
return filtered.first()
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs)
class Host(models.Model):
created = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
name = models.CharField(unique=True, max_length=settings.MAX_CHAR_COUNT)
ip_addresses = models.ManyToManyField(IPAddress)
services = models.ManyToManyField(Service)
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
os = models.ForeignKey(OperatingSystem, on_delete=models.CASCADE, blank=True, null=True)
ciphers = models.ManyToManyField(Cipher, blank=True)
certificate = models.ForeignKey(Certificate, on_delete=models.CASCADE, blank=True, null=True)
class Meta:
ordering = ['name']
设置
pk\u url\u kwarg
属性,并覆盖视图的get\u queryset(…)
方法
class HostDetailPageView(DetailView):
template_name = 'hosts/hostdetail.html'
model = Host
pk_url_kwarg = 'name'
def get_queryset(self):
return super().get_queryset().filter(host__name=self.kwargs['host'])
class HostDetailPageView(详细视图):
模板名称='hosts/hostdetail.html'
模型=主机
pk_url_kwarg='name'
def get_queryset(自我):
return super()definition@ArakkalAbu我的目标是让URL看起来像/domains/example.com/host.example.com
,主机名的值存储在哪个字段?在URL模式中,它将是最后一个值
。在主机
模型中,它被称为名称
。