我想限制用戶在Django中訪問某些頁面直到他們登錄。下面的代碼工作正常,除了我用來傳遞查詢設置它可以顯示我的所有書籍,而且這種方法不能像那樣工作。在Django中傳遞在保護視圖中設置的查詢
新的視圖類(需要登錄,但沒有查詢集送?)
class IndexView(TemplateView):
template_name = 'quotes/index.html'
args = Book.objects.all()
@method_decorator(login_required)
def dispatch(self, args):
return super(IndexView, self).dispatch(args)
我無法弄清楚如何使用這種類型的視圖一起傳遞我的查詢Book.objects.all()
。我的看法過去看起來像下面。正如你所看到的,它返回了一個由模板使用的查詢集。 我正在使用此Django documentation來嘗試並提供登錄功能。
以前使用的視圖類(無需登錄):
class IndexView1(generic.ListView):
template_name='quotes/index.html'
context_object_name = 'book_list'
def get_queryset(self):
"""Return all the book objects"""
return Book.objects.all()
我不明白你在這裏做了什麼。爲什麼你不能繼續使用ListView子類並在那裏重寫'dispatch()'?或者爲什麼你不能在urlconf中使用login_required包裝器,如[文檔]中所示(https://docs.djangoproject.com/en/1.6/topics/class-based-views/intro/#decorating-class基於視角)? –
看起來我被文檔弄糊塗了。我沒有意識到你可以使用這種派遣。 – lorless