Subhajeet Dey
Subhajeet Dey

Reputation: 100

How to return from another function in Django View function

I have a function in views.py

def login(request):
   actor = LoginActor(request)
   actor.authenticated_user() # Cannot use return here, this is problematic, we need to redirect here without using a return statement

   ctx = actor.get()

   if request.method == 'POST':
      ctx = actor.post()

      return render(request, 'user/forms/auth.jinja', ctx)
   return render(request, 'user/login.jinja', ctx)

and there is a redirect in authenticated_user() function which is defined as:

def authenticated_user(self):
    if self.request.user and self.request.user.is_authenticated:
        return redirect('home')

How do i return from the initial view without calling a return, basically i want to return the callee function where there is a return in called function I am using Django 2.1 with Python 3.7

Upvotes: 0

Views: 5646

Answers (3)

abdelwaheb moalla
abdelwaheb moalla

Reputation: 216

Yo should use classes instead of functions to handle requests in fact in your case you can do this

class LoginView(View):

    login_url = "your login url"
    path = "your path to login template"

    def get(self, request):
        if request.user.is_authenticated():
            return redirect('home')
        else:
            return render(request, self.path)

    def post(self,request):
        #treatment here
        actor = LoginActor(request)
        #I am not sure how you defined LoginActor but should return None if user is not logged in otherwise it returns a logged in user I can help you more if you provide how you defined ActorLogin()
        if actor:
             redirect('home')
        return render(request, self.path,{"error_message":"couldn't login in"})`

Now you add LoginRequiredMixin in your other views so you don't need to check if the user is logged in or use use UserPassesTest if you need other requirments than user login in. for more details check the official documentation about login

https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin

Upvotes: 0

gurpreet singh chahal
gurpreet singh chahal

Reputation: 370

Django has decorators for this purpose. For example To allow only logged-in users to view the home page,

from django.contrib.auth.decorators import login_required

@login_required(login_url='/accounts/login/')
def home(request):
    ## How home page for only registered users

You can create a custom decorator to validate guest(non-logged in) users. Refer to https://medium.com/@MicroPyramid/custom-decorators-to-check-user-roles-and-permissions-in-django-ece6b8a98d9d for help on creating a custom decorator.

Upvotes: 0

blhsing
blhsing

Reputation: 106598

You should leave both rendering and redirection to the view function, and have your utility function authenticated_user return just a Boolean value instead:

def authenticated_user(self):
    return self.request.user and self.request.user.is_authenticated

def login(request):
   actor = LoginActor(request)
   if actor.authenticated_user():
       return redirect('home')

   ctx = actor.get()

   if request.method == 'POST':
      ctx = actor.post()

      return render(request, 'user/forms/auth.jinja', ctx)
   return render(request, 'user/login.jinja', ctx)

Upvotes: 2

Related Questions