I'm developing a web application, and I need something like this:
我正在开发一个Web应用程序,我需要这样的东西:
url(r'^$', 'collection.views.home', name='home'),
url(r'^$', 'collection.views.main', name='main'),
If the user is authenticated, go to main, otherwise go to home. On the home page, differently there will be a sign in a button. But these should be on the same URL pattern.
如果用户已通过身份验证,请转到main,否则请转到home。在主页上,不同的是按钮中会有一个标志。但这些应该是相同的URL模式。
How can I handle it?
我该怎么处理?
2 个解决方案
#1
7
To handle that kind of thing you can use a single view that follows different code paths depending on the request state. That can mean something simple like setting a context variable to activate a sign in button, or something as complex and flexible as calling different functions - eg, you could write your home and main functions, then have a single dispatch view that calls them depending on the authentication state and returns the HTTPResponse object.
要处理这种事情,您可以使用遵循不同代码路径的单个视图,具体取决于请求状态。这可能意味着像设置上下文变量以激活登录按钮一样简单,或者像调用不同函数那样复杂和灵活的东西 - 例如,您可以编写您的home和main函数,然后有一个调用它们的调度视图取决于身份验证状态并返回HTTPResponse对象。
If authentication is all you need to check for, you don't even need to set a context variable - just use a RequestContext
instance, such as the one you automatically get if you use the render
shortcut. If you do that, request
will be in the context so your template can check things like {% if request.user.is_authenticated %}
.
如果您需要检查身份验证,则甚至不需要设置上下文变量 - 只需使用RequestContext实例,例如您使用渲染快捷方式时自动获取的实例。如果您这样做,请求将在上下文中,因此您的模板可以检查{%if request.user.is_authenticated%}之类的内容。
Some examples:
def dispatch(request):
if request.user.is_authenticated:
return main(request)
else:
return home(request)
Or for the simpler case:
或者对于更简单的情况:
def home(request):
if request.user.is_authenticated:
template = "main.html"
else:
template = "home.html"
return render(request, template)
#2
0
While looking into a similar problem, I found an application - django-multiurl. It has its limitations, but it is very convenient and useful.
在研究类似的问题时,我找到了一个应用程序--django-multiurl。它有其局限性,但它非常方便和有用。
Basically it allows you to raise a ContinueResolving
exception in a view, which will result in next view being processed.
基本上它允许您在视图中引发ContinueResolving异常,这将导致处理下一个视图。
#1
7
To handle that kind of thing you can use a single view that follows different code paths depending on the request state. That can mean something simple like setting a context variable to activate a sign in button, or something as complex and flexible as calling different functions - eg, you could write your home and main functions, then have a single dispatch view that calls them depending on the authentication state and returns the HTTPResponse object.
要处理这种事情,您可以使用遵循不同代码路径的单个视图,具体取决于请求状态。这可能意味着像设置上下文变量以激活登录按钮一样简单,或者像调用不同函数那样复杂和灵活的东西 - 例如,您可以编写您的home和main函数,然后有一个调用它们的调度视图取决于身份验证状态并返回HTTPResponse对象。
If authentication is all you need to check for, you don't even need to set a context variable - just use a RequestContext
instance, such as the one you automatically get if you use the render
shortcut. If you do that, request
will be in the context so your template can check things like {% if request.user.is_authenticated %}
.
如果您需要检查身份验证,则甚至不需要设置上下文变量 - 只需使用RequestContext实例,例如您使用渲染快捷方式时自动获取的实例。如果您这样做,请求将在上下文中,因此您的模板可以检查{%if request.user.is_authenticated%}之类的内容。
Some examples:
def dispatch(request):
if request.user.is_authenticated:
return main(request)
else:
return home(request)
Or for the simpler case:
或者对于更简单的情况:
def home(request):
if request.user.is_authenticated:
template = "main.html"
else:
template = "home.html"
return render(request, template)
#2
0
While looking into a similar problem, I found an application - django-multiurl. It has its limitations, but it is very convenient and useful.
在研究类似的问题时,我找到了一个应用程序--django-multiurl。它有其局限性,但它非常方便和有用。
Basically it allows you to raise a ContinueResolving
exception in a view, which will result in next view being processed.
基本上它允许您在视图中引发ContinueResolving异常,这将导致处理下一个视图。