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

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

问题描述

我正在开发一个具有两种类型用户的网站,并且该项目的所有者在用户通过身份验证后想要两个不同的家庭(模板),因此,我尝试了以下方法:

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:

url

# 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! (家庭A)我通过控制台打印了一个小旗,条件条件起作用,然后通过会话:家庭-a或家庭-b,但始终转到家庭-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')

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

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