1

Django's login_required decorator requires a login before proceeding to any functionality in a view function. If a user is not logged in, they will be redirected through a login screen.

If the HTTP request is a GET, Django sometimes puts the GET destination in a parameter ("next"). Or, Django also has a LOGIN_REDIRECT_URL setting that is a URL a user is forwarded to after login.

If the HTTP request is a POST, that POST data is lost during the login process.

What is the best way to preserve the original URL and POST data for the original POST request, and re-post that data after the user is logged in?

I imagine putting something in a session, but I'd have to intercept the POST data and URL on the way through login_required. The best practice is not obvious to me.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • Why not have the user login before it can view the form? – Vincent May 14 '21 at 22:06
  • 2
    This [S.O question](https://stackoverflow.com/questions/62049176/django-form-content-lost-when-redirecting-using-login-required) might be useful. – Frederic Perron May 15 '21 at 00:03
  • may be prevent default post behaviour? pop up a login modal, login using ajax and continue with the same form? – ItsMilann May 15 '21 at 01:56
  • @Vincent Because we want to show people what is possible without the friction of a user account. No one wants to create an account when they don't know why to do it. So our interface is open until they do something that requires an account. – dfrankow May 16 '21 at 01:37
  • @FrédéricPerron I think this is probably the answer. Save the POST data in a session before logging in. I think I'd have to write a new `@my_login_required` decorator to accomplish that, but I think it's the only way. – dfrankow May 16 '21 at 01:39

0 Answers0