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?

halfer
  • 19,824
  • 17
  • 99
  • 186
user1403505
  • 895
  • 2
  • 19
  • 42
  • 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. – Carcigenicate Mar 24 '21 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 – user1403505 Mar 25 '21 at 05:14

1 Answers1

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)
Kovy Jacob
  • 489
  • 2
  • 16
  • 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. https://stackoverflow.com/questions/66707080/maximum-recursion-depth-exceeded-when-using-singleton-logging-adapter-in-django – user1403505 Mar 25 '21 at 05:11
  • 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). – Kovy Jacob Mar 25 '21 at 14:13