[英]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
你可以試試這個:
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.