簡體   English   中英

需要 Django wagtail 登錄不起作用

[英]Django wagtail login required not working

我正在研究 wagtailcms。我只想在用戶登錄時顯示一個頁面。我嘗試過 LoginRequiredMixin,@method_decorator(login_required, name='dispatch') 但沒有任何效果。請幫忙

from django.contrib.auth.decorators import login_required
@login_required(login_url='/login/')
class LessonListPage(RoutablePageMixin, Page):
""" Lists all the lessons on one page """

    template = "lesson/lesson_list_page.html"

    custom_title = models.CharField(
    max_length=100,
    blank=False,
    null=False,
    help_text='Overwrites the default title',
    )

    content_panels = Page.content_panels + [
    FieldPanel("custom_title"),
    ]

    def get_context(self, request, *args, **kwargs):
        """Adding custom stuff to our context"""
        context = super().get_context(request, *args, **kwargs)
        context["posts"] = LessonDetailPage.objects.live(
        ).public().order_by('-first_published_at')[:10]
        #Change 'public' to adjust which posts are displayed
        context["postcategories"] = Category.objects.all()
        return context

在 Wagtail 頁面模型上, serve方法相當於一個視圖函數,因此覆蓋該方法並添加一個method_decorator應該可以工作。

但是,Wagtail 已經內置了此功能:通過管理員創建頁面時提供 的隱私控制允許您將頁面設置為僅供登錄用戶訪問。

你可以試試這個:

class LessonListPage(RoutablePageMixin, Page):
""" Lists all the lessons on one page """

    template = "lesson/lesson_list_page.html"

    custom_title = models.CharField(
    max_length=100,
    blank=False,
    null=False,
    help_text='Overwrites the default title',
    )

    content_panels = Page.content_panels + [
    FieldPanel("custom_title"),
    ]

    def get_context(self, request, *args, **kwargs):
        """Adding custom stuff to our context"""
        if request.user.is_authenticated:
           context = super().get_context(request, *args, **kwargs)
           context["posts"] = LessonDetailPage.objects.live(
    ).public().order_by('-first_published_at')[:10]
           #Change 'public' to adjust which posts are displayed
           context["postcategories"] = Category.objects.all()
           return context
        else:
           return redirect('/')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM