0

I am have a django project that I am creating. Within the django project I have two apps which as user and common. I want to use a method that validates if a user is logged in from the common apps views.py file within the user views.py file. How can I can the LoggedIn method from the common view.py in a method from he user views.py Home method....

User -> views.py file:

# checks if someone is logged in
def Home(request):
    # check if there is a user logged in
    currentUser = LoggedIn(request)
    # no user logged in
    if currentUser == None:
        # send user to the main sign in and log in form
        return redirect('Signup')
    # a user logged in
    else:
        # users home page content will be down below
        parameters = 'parameters'

Common -> views.py file:

def LoggedIn(request):
    if 'username' not in request.session:
        return None
    else:
        user = request.session['username']
        currentUser = User.objects.get(username=username)
        return curentUser

I cant seem to get how to call one method from a different app. I don't want to have to redirect the user but rather just call the method and return a value to the Home method in the user app views.py file. Couldn't find any documentation on this matter.

Omar Jandali
  • 814
  • 3
  • 20
  • 52

1 Answers1

0

You are trying to reinvent the wheel there. Django has built-in authentication support which does what you are trying to do properly. I'd do a bit more reading before trying to code something, start here: https://docs.djangoproject.com/en/1.11/topics/auth/default/

Specifically, you seem to be looking for login_required decorator.

If your question was conceptual, you are looking for a way to import a function from another file, in that case check this question.

Also, Python has a style guide for coding, called PEP. For example, function names are suggested to be lowercase and words separated with underscores. I suggest you check that, too: https://www.python.org/dev/peps/pep-0008/#function-names

Gokhan Sari
  • 7,185
  • 5
  • 34
  • 32