cookiecutter-flask/cookiecutter-flask

bug Login route in nav.html for subdirectory apps

glyg opened this issue · 3 comments

glyg commented

Project configuration:

Option Values
use_pipenv
  • yes
  • no
python_version
  • 3.8
  • 3.7
  • 3.6
node_version
  • 12
  • 10
use_heroku
  • yes
  • no
Are you using Docker to run the app?
  • yes
  • no

I'm making an app that runs on a sub-directory of my website (https://example.com/myapp/)

Due to the action="/" on the login form in nav.html, when the user tries to login the POST is directed to the website root directory, which is a 404 for the app, and prevents logging to go through:

flask-dev_1   | [FLASK] 172.24.9.25 - - [09/Oct/2020 14:34:52] "POST / HTTP/1.1" 404

Removing the slash and putting an empty string here fixes the problem - although I'm not sure it is the right thing to do (maybe get request.path instead?

Thanks for the cookie cutter for all the rest, it's a huge time saver!

I can open a PR if you need

Best

Guillaume

Hi @glyg!

This is an issue for you because your application is not being served at the domain root. Setting action="" partially works, but would not be the best implementation. This is because when the action is empty, the POST payload is sent to the current page. Login POST requests are handled by the root endpoint, so all requests will be sent to the right place as long as you log in from the home page. You will encounter problems, though, when you try to log in from any other page. If you go to the "about" page, for example, and try to log in, the form with post the data to the /about endpoint, which doesn't allow POST requests and return an error code.

The alternative is to use jinja template functions to template in the correct url. The correct one for this situation is {{ url_for('public.home') }} (you can find a few other places where it's used to see what value it returns). This will be the most reliable approach, as it doesn't rely on any hardcoded values. If you wanted to put up a PR to make this change, I'd be more than happy to merge it in!

glyg commented

Thanks for the response,

PR follows :)

🎉 Sounds great! A PR would be very welcome. 😄