我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用django.views.generic.detail.DetailView()。
def get_queryset(self): # Hack: get a random object from this model, get the parents model, # use that to query for the parent, use that to query children to get # the list we want # the alternative is just to use a DetailView to get the parent model and object # then just use the children from that object # I decided to do this because the "model" of this view should be the list we display # then the other forms and things associated with this view will correspond with the model. parent_pk = self.request.GET.get('parent_pk') random_object = self.model.objects.first() ParentModel = random_object.parents.model parent = ParentModel.objects.get(pk=parent_pk) qs = parent.children return qs
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) context['user'] = self.request.user context['users'] = User.objects.all() # Send list of keys to front end if the request came from the admin if self.request.user.is_superuser: keys = InviteKey.objects.all() context['keys'] = keys return context # Use Django's built in login system but redirect to the homepage if already # logged in