两个不同视图的一个 url [英] One url for two different views

查看:32
本文介绍了两个不同视图的一个 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有两种类型用户的网站,并且项目所有者在用户通过身份验证后想要两个不同的主页(模板),所以,我尝试了这个:

I'm developing a site that have two types of User, and the project's owners want two different home (templates) after the user is authenticated, so, I tried this:

网址

# home a
url(r'^home/$', HomeAView.as_view(), name='home-a'),
# home b
url(r'^home/$', HomeBView.as_view(), name='home-b'),

在我的登录视图中也有类似的内容:

And some like that at into my log_in view:

if user.typeUser == "HA":
    print('Go to Home A')
    return redirect(reverse('sesion:home-a'))
else:
    print('Go to Home B')
    return redirect(reverse('sesion:home-b'))

那么问题来了,用户认证后,网站总是去第一个url!(Home A)我通过控制台打印了一个小标志并且条件正在工作,并且通过了 sesion:home-a 或 home-b,但总是去 home-a.如果我分配了不同的名称,为什么反向不解析 url?我不能有两个视图的一个网址吗?

So the problem is that after the user is authenticated, the site always go to the first url! (Home A) I printed by console a little flag and the conditional is working, and pass by sesion:home-a or home-b, but always go to home-a. Why reverse don't resolve the url, if I assigned different names? Can't I have one url for two views??

感谢您的帮助,我正在开发 Django 1.7

Thanks for your help, I'm working on Django 1.7

推荐答案

不,您不能对同一个 URL 有两个不同的视图.Django根据URL和定义的路由决定使用哪个视图函数.

No, you cannot have two different views for the same URL. Django decides which view function to use according to the URL and the routes defined.

在独特的视图中使用自定义代码根据用户类型呈现不同的模板:

Use custom code inside an unique view to render a different template depending on the type of user:

def home(request):
    if user.typeUser == "HA":
        render(request, 'template_a.html')
    else:
        render(request, 'template_b.html')

这篇关于两个不同视图的一个 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆