0

So I have created a Django-app with a login form and using login(request, user) in my view.py to login the user. This is the main page and has the location mypage.com. I also created another function in my view.py called log_out:

def log_out(request):
    logout(request)
    return redirect('/') 

This function is used to log out a user using the url: mysite.com/logout as defined in my url.py:

url(r'^logout/', views.log_out, name='log_out'),

This is working to log out the user, however the user is being logged out as I'm typing in the logout-url into the address-field of my browser, and before hitting enter to go to that endpoint.

I would like it to not log out the user when typing in the url, and wait until I'm "entering" the site.

Can anyone help me with this one?

magnusnn
  • 137
  • 1
  • 13
  • Your site can't log you out unless you actually visit the `logout` URL. Are you sure you're being logged out *"as you're typing the URL...before hitting Enter"*? – xyres Apr 23 '17 at 13:54
  • Yes, I'm sure. As I'm typing in the url in the browser, my server responded with `"GET /logout/ HTTP/1.1" 302 0` before I even touched the enter-button – magnusnn Apr 23 '17 at 13:58
  • I guess the `302` is some kind of `found the url`-message. Is there a way to say something like `if request.GET.statuscode == 301: DO-MY-CODE`. If I'm not mistaken the responsecode 301 is that you actually visits the site? – magnusnn Apr 23 '17 at 14:06
  • Do you *see* your browser making the request in your browser's network log while you're typing the URL? Also, what browser are you using? – xyres Apr 23 '17 at 14:17
  • You should use POST to avoid pre-fetch browser behavior. Take a look: http://stackoverflow.com/a/14587231 – dani herrera Apr 23 '17 at 14:19
  • It's a _response_ status code, not a _request_ status code. Something like `if request.GET.statuscode == 301:` makes absolutely no sense, because there is no status code until you actually return a response. The status code is `302` because you use the `redirect()` function. Both `301` and `302` signal the browser to redirect to a different page, but `301` may be cached. – knbk Apr 23 '17 at 17:18

2 Answers2

1

What i am doing is redirecting the user to my login page.So I should say you should try this.My application only logouts after I hit the enter button.

url:-

app_name = 'name'
url(r'^logout$', user.logout_user, name='logout_user'),
 url(r'^$', user.login_user, name='login'),

views:-

def logout_user(request):
    logout(request)
    return redirect('name:login')
Abi Waqas
  • 299
  • 1
  • 13
  • Well, this is exactly what I have in my application. Guess my only option is to use POST to avoid pre-fetch browser behavior as @danihp suggested in the comments above.. – magnusnn Apr 24 '17 at 11:42
  • can you show me the view method assosicated with this url(r'^/').i mean u have this url defined or not – Abi Waqas Apr 24 '17 at 11:48
  • this is the root of my application. say mypage.com. This is where a login-form is displayed and the url for this one is `url(r'^$', views.login_view, name='login_view')` and everything related to the authentication of a user is defined in the `login_view`-function in my view. – magnusnn Apr 24 '17 at 11:51
  • i have updated my answer use the app_name method to redirect to login like('name:login') instead of ('/') – Abi Waqas Apr 24 '17 at 11:53
0

I ended up using a POST-method as suggested by @danihp.

magnusnn
  • 137
  • 1
  • 13