Django,根据请求切换视图

时间:2022-01-16 14:45:11

I want to display different thing at the same URL (home page) depending on whether a user is logged in or not.

我想在同一个URL(主页)上显示不同的内容,具体取决于用户是否登录。

So, if he is not authenticated I'll display a page which does not involve any DB query, otherwise, if he is logged in, I'll display his projects (this involves DB access).

因此,如果他未经过身份验证,我将显示一个不涉及任何数据库查询的页面,否则,如果他已登录,我将显示他的项目(这涉及数据库访问)。

So, how can I accomplish this, given that:

那么,我怎样才能做到这一点,因为:

  • I have only 1 url pattern
  • 我只有1个url模式

  • I want to use class based generic views (specifically, TemplateView in one case, DetailView in the other).
  • 我想使用基于类的通用视图(具体来说,在一种情况下是TemplateView,在另一种情况下是DetailView)。

2 个解决方案

#1


1  

Check to see if User.is_authenticated(), if they are, query for the projects, if not don't query for the projects, and in the view see if the projects variable is set or not.

检查User.is_authenticated(),如果是,查询项目,如果不查询项目,并在视图中查看是否设置了项目变量。

#2


0  

Generally one would use the Login Required decorator but since you only have a single URL you could check whether request.user.is_authenticated() is True. If it's True return the template corresponding to the logged in user, otherwise return the other one.

通常会使用Login Required装饰器,但由于您只有一个URL,因此可以检查request.user.is_authenticated()是否为True。如果为True,则返回与登录用户对应的模板,否则返回另一个。

Class Based Views

基于类的视图

Take a look at this: login required in TemplateView

看看这个:在TemplateView中需要登录

It used the dispatch method to check for a user being authenticated in a class based view.

它使用dispatch方法检查在基于类的视图中进行身份验证的用户。

from django import http from django.views import generic

来自django的导入http来自django.views import generic

class AboutView(generic.TemplateView):
    """ About page view. """
    template_name = 'about.html'

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            raise http.Http404
        return super(AboutView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        ctx = super(AboutView, self).get_context_data(**kwargs)
        ctx['something_else'] = None  # add something to ctx
        return ctx

#1


1  

Check to see if User.is_authenticated(), if they are, query for the projects, if not don't query for the projects, and in the view see if the projects variable is set or not.

检查User.is_authenticated(),如果是,查询项目,如果不查询项目,并在视图中查看是否设置了项目变量。

#2


0  

Generally one would use the Login Required decorator but since you only have a single URL you could check whether request.user.is_authenticated() is True. If it's True return the template corresponding to the logged in user, otherwise return the other one.

通常会使用Login Required装饰器,但由于您只有一个URL,因此可以检查request.user.is_authenticated()是否为True。如果为True,则返回与登录用户对应的模板,否则返回另一个。

Class Based Views

基于类的视图

Take a look at this: login required in TemplateView

看看这个:在TemplateView中需要登录

It used the dispatch method to check for a user being authenticated in a class based view.

它使用dispatch方法检查在基于类的视图中进行身份验证的用户。

from django import http from django.views import generic

来自django的导入http来自django.views import generic

class AboutView(generic.TemplateView):
    """ About page view. """
    template_name = 'about.html'

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            raise http.Http404
        return super(AboutView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        ctx = super(AboutView, self).get_context_data(**kwargs)
        ctx['something_else'] = None  # add something to ctx
        return ctx