Fix #248: Fix next param logic and move logic to Python.

By deciding whether to redirect the user to the next param URL or the
default URL in Python instead of JavaScript, we can rely on Django’s
is_safe_url logic.
I kept it private because I don't want anyone trying to import it and use it and expect it to be a stable API. The underscore basically signals "This isn't part of our public API, if you try to use it in your own code expect it to go away without notice, even if we're post-1.0."