2
我試圖根據參數中的值使用詳細視圖。Django:通用細節視圖IDView必須使用對象pk或子彈來調用
網址:
url(r'^mousesmall/(?P<name>.*)/$', IDView.as_view()),
觀點:
class IDView(DetailView):
model = RNA
template_name = "home/details.html"
def get_context_data(self, **kwargs):
context = super(IDView, self).get_context_data(**kwargs)
data = self.kwargs['name']
context['object'] = RNA.objects.filter(rna_id=data)
return context
我不太確定如何調用與對象PK的看法。
你在解決方案中設置了slug_field兩次。在我的模板中翻轉它的目的是什麼?另外,如果這有所幫助,我的rna_id與我的主鍵不一樣。 – dl8
對不起,這是一個錯字。反轉意味着獲取該對象的URL。你可以使用'url'模板標籤來查看你寫的視圖。在您的網址上使用「name」屬性時更容易完成。另一種方法是使用['get_absolute_url'](如果你的'IDView'有一個特定的url,你可以通過添加一個['get_absolute_url'](https://docs.djangoproject) .com/en/dev/ref/models/instances /#get-absolute-url)方法添加到你的類中。這就是你可以簡單地使用'myobject.get_absolute_url'來生成一個到你的頁面的鏈接 –