-1

When a request come for /page-one url, I'd like to use view_a if the user is authenticated and view_b for the guest visitors.

The code should be like this:

def dummy(request):
    if request.user.is_authenticated():
        print 'authuser' 
        return view_a(request) 
    else: 
        print 'unauth user'
        return view_b(request)

How can I achieve this in django?

I've looked at the docs but could not find any relevant guides about this.

qliq
  • 11,695
  • 15
  • 54
  • 66
  • 1
    Replace this pseud-code with a wrapper-function. Which would be a generic/dummy view doing nothing but choosing the right view. Just wrap your code with a function. That's it. – Ivan Starostin Apr 04 '19 at 18:29

2 Answers2

2

You can use is_authenticated Use this in views

if user.is_authenticated:
    return render(request, 'polls/detail.html', {'poll': p})
else:
    return render(request, 'polls/another-detail.html', {'poll': p})
shafik
  • 6,098
  • 5
  • 32
  • 50
  • This is for different templates `inside` a view. My question is about using `different views`. – qliq Apr 04 '19 at 18:17
  • 1
    So what's the problem? `def generic_view(request): if user.is_authenticated: return specific_view(request) else: return another_view(request)` @qliq – Ivan Starostin Apr 04 '19 at 18:19
  • @IvanStarostin, for performance purposes, I need to use separate views. – qliq Apr 04 '19 at 18:22
  • Take another look at my comment. There are three views. @qliq – Ivan Starostin Apr 04 '19 at 18:23
  • @IvanStarostin I don't use generic views, please update your answer based on old-fashioned urls-views, as per my question. – qliq Apr 04 '19 at 18:27
  • This is the direct answer to your question. There are no "generic views" in term of Django framework. – Ivan Starostin Apr 04 '19 at 18:29
  • 1
    Your whole question is based on a total misunderstanding if you think there is any "performance" difference in using separate views. – Daniel Roseman Apr 04 '19 at 18:38
  • @DanielRoseman the matter is that I want to cache the view for unauthenticated users and I don't want it to interfere with the ux of registered users. I could not do that in a single view, so I want to have a separate view for that. – qliq Apr 04 '19 at 18:50
  • You certainly *can* do that in a single view. In any case, Ivan has given you the answer. – Daniel Roseman Apr 04 '19 at 18:52
  • @DanielRoseman, this is my last resort as there are a couple of questions here, where people could not cache based on authentication in a single view, so that would be great if you could answer them: like https://stackoverflow.com/questions/55521102/how-to-cache-a-view-only-for-unauthenticated-users or https://stackoverflow.com/questions/34154730/how-to-cache-django-views-only-for-unauthenticated-users – qliq Apr 04 '19 at 18:55
1

To redirect to different views (not just render different templates)

from django.urls import reverse
from django.http import HttpResponseRedirect

...

if user.is_authenticated:
    return HttpResponseRedirect(reverse('some_detail', kwargs={'pk': pk}))
else:
    return HttpResponseRedirect(reverse('another_detail', kwargs={'pk': pk}))

and in your urls you would have to 'name' the view

path('another_detail/<int:pk>/', views.another_detail, name='another_detail'),
amchugh89
  • 1,276
  • 1
  • 14
  • 33