0

I am using a Django-based web application in Python 3.

The logout method will redirect me to a different view and it will also call sys.exit("str")

def logout(request):
  try:
    
    logger.info("Logging Out")
    del request.session['role']
    del request.session['email']
    del request.session['fullname']
    del request.session["token"]
    session.close()
    sys.exit("bye")
    return redirect("/login")
   
  except Exception as e:
    logger.info("Exception : {}".format(e))

The above code was redirecting me as expected. Recently I introduced iframes in the template html, so that the pages are rendered in iframe when clicked from a side menu navigation.

<div class="sidenav">
  <a href="lkpview" target="iframe1">About</a>
  <a href="logout" >Sign out</a>
  
</div>

<div class="main">
    <iframe width="90%" height="300" name="iframe1">
    </iframe>

</div>

Now if click on "Sign out" I am getting this error:

A server error occurred.  Please contact the administrator.

Not sure of this, and it did not occur before I introduced the iframe to the Django template. What can I try next?

2
  • 1
    I have no experience with Django, but calling sys.exit there doesn't make much sense. That will kill your program. That return redirect("/login") that follows it will never be reached. Commented Mar 24, 2021 at 16:31
  • Yes maybe you are correct, but it redirected after using sys.exit() not sure how before using iframes, sys.exit() did not terminate my program on the command line, until i pressed CTRL+C Commented Mar 25, 2021 at 5:14

1 Answer 1

1

It looks like you probably have 'debug' set to 'false' in settings.py, because you should be getting a lot more info than that. But anyway, sys.exit() isn't the right command, django has an auth module that can log users out and then redirect them. using sys.exit() isn't really logging them out, its like beheading them. Use this:

from django.contrib.auth import logout

def logout_view(request):
    logout(request)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Thanks for your input, I am not using Django's input authentication , I am using my own custom database, and I am logging in through a different system. Will this logout still work for that? Also why i used sys.exit() was to solve this problem of LoggingAdapter object not getting destoryed, i described here. stackoverflow.com/questions/66707080/…
I'm not sure, I don't know enough to answer that. However, a) I don't understand why using a custom database would prevent you from using Django's auth module. As an aside, the whole point of django is to have everything in one simple system, and it is one of the easiest web frameworks. I would recommend using the django auth system and expanding it to meet your needs (you can).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.