英文:
How to display a query on DetailView in Django?
问题
我想让我的 "CustomDetailView" 显示一个查询(一个单独的 "flashcard")。我能够通过使用 ListView 来实现它。
CustomListView(ListView):
model = Flashcard
template_name = 'flashcards.html'
queryset = Flashcard.objects.all()[:1]
但是对于 DetailView,我遇到了以下错误:
通用详细视图 CustomDetailView 必须在 URLconf 中使用对象的主键(pk)或 slug 中的一个进行调用。
class CustomDetailView(DetailView):
model = Flashcard
template_name = "flashcards.html"
queryset = Flashcard.objects.all().first()
urls.py:
path('', CustomDetailView.as_view(), name='flashcards')
如何修复这个问题?
英文:
I want my "CustomDetailView" to display a query(a single "flashcard"). I was able to it by using ListView
CustomListView(ListView):
model = Flashcard
template_name = 'flashcards.html
queryset = Flashcard.objects.all()[:1]
But for DetaiView I'm getting this error
Generic detail view CustomDetailView must be called with either an object pk or a slug in the URLconf.
class CustomDetailView(DetailView):
model = Flashcard
template_name = "flashcards.html"
queryset = Flashcard.objects.all().first()
urls.py
path('', CustomDetailView.as_view(), name='flashcards'),
How to fix this?
答案1
得分: 3
从查询集中删除 first:
class CustomDetailView(DetailView):
model = Flashcard
template_name = "flashcards.html"
queryset = Flashcard.objects.all().exclude(first=True)
lookup_field = 'pk'
lookup_url_kwarg = 'pk'
并将 id 添加到 URL:
path('<int:pk>/', CustomDetailView.as_view(), name='flashcards'),
英文:
remove first from queryset:
class CustomDetailView(DetailView):
model = Flashcard
template_name = "flashcards.html"
queryset = Flashcard.objects.all()
lookup_field = 'pk'
lookup_url_kwarg = 'pk'
enter code here
and add id to url:
path('/<int:pk>/', CustomDetailView.as_view(), name='flashcards'),
答案2
得分: 0
以下是您需要的翻译部分:
-
You need your
pk
orslug
in:
您需要在以下位置使用pk
或slug
: -
This is because DetailView is inheriting from SingleObjectMixin, and it fetches objects that way. If you want, jump to definition of this class in your IDE to see its implementation of
get_queryset
andget_object
.
这是因为DetailView继承自SingleObjectMixin,并通过这种方式获取对象。如果您愿意,可以在您的IDE中跳转到此类的定义,以查看其get_queryset
和get_object
的实现方式。 -
How you call your
pk
orslug
really depends on your model, theFlashcard
. If yourprimary_key
field is anInteger
, I think you'll be fine writing:
如何调用您的pk
或slug
取决于您的模型,即Flashcard
。如果您的primary_key
字段是一个Integer
,我认为您可以这样编写: -
edit: as Pourya Mansouri wrote, you do need to remove queryset attribute in your case too.
编辑: 正如Pourya Mansouri写的,在您的情况下,您确实需要删除queryset
属性。 -
The
slug
you can customize with slug_field and similar attributes.
您可以使用slug_field和类似的属性来自定义slug
。 -
You can define a completely custom object fetching behavior by overriding get_object or get_queryset. Here's a random examples of doing that I found on the internet:
您可以通过覆盖get_object或get_queryset来定义完全自定义的对象获取行为。以下是我在互联网上找到的一些示例:
英文:
You need your pk
or slug
in
path('', CustomDetailView.as_view(), name='flashcards'),
See an example from the docs:
urlpatterns = [
path('<slug:slug>/', ArticleDetailView.as_view(), name='article-detail'),
]
This is because DetailView is inheriting from SingleObjectMixin, and it fetches objects that way. If you want, jump to definition of this class in your IDE to see its implementation of get_queryset
and get_object
.
How you call your pk
or slug
really depends on your model, the Flashcard
. If your primary_key
field is an Integer
, I think you'll be fine writing
path('<int:pk>', CustomDetailView.as_view(), name='flashcards'),
edit: as Pourya Mansouri wrote, you do need to remove queryset attribute in your case too
The slug
you can customize with slug_field and similar attributes.
You can define a completely custom object fetching behavior by overriding get_object or get_queryset. Here's a random examples of doing that I found on the internet:
- Overriding
get_object
: https://www.valentinog.com/blog/detail/ - Overriding
get_queryset
: https://www.agiliq.com/blog/2019/01/django-when-and-how-use-detailview/
答案3
得分: 0
也许应该更好地沟通。对不起我的英语。
当我点击链接并显示在detailView上时,我想返回一个随机查询。我通过以下方式实现了它。不认为它很高效。如果有人有任何想法,请分享。
def get_object(self):
queryset = Flashcard.objects.order_by('?').first()
return queryset
英文:
Maybe should have better communicated. Sorry for my English.
I wanted to return a random query when I clicked on a link a show it on detailView. I was able to it by this. Don't think it's efficient. If anyone has any idea share it.
def get_object(self):
queryset = Flashcard.objects.order_by('?').first()
return queryset
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论