【问题标题】:Adding start separate page in django app在 django 应用程序中添加开始单独的页面
【发布时间】:2015-03-11 16:00:42
【问题描述】:

我想在我的 Django 应用程序中添加一个单独的主页。 如果用户登录,则向他显示所有内容。 如果用户未登录,则向他显示起始页和登录链接。

解决方法是基于下面的代码并不总是通过考试。

{% if user.is_authenticated  %}

//Something

{% else %}

//Something

{% endif %}

我的看法:

class Start(TemplateView):
    template_name = "dashboard/start.html"

【问题讨论】:

    标签: python django templates authentication


    【解决方案1】:

    您可以覆盖视图的get_template_names() 方法并为匿名用户和登录用户呈现不同的模板:

    class Start(TemplateView):
    
        def get_template_names(self):
            if self.request.user.is_authenticated():
                return ['dashboard/dashboard.html']
            return ['dashboard/start.html']
    

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多