I have a Django Web site where I’m using the oauth2app library for OAuth 2 authentication. I’ve also modified oauth2app to use crispy_forms, instead of the deprecated uni_form.
The authorize() method in oauth2app is guarded by a Django decorator, @login_required. Indeed, if a client tries to authorize without first logging in, the login page appears.
As expected, the decorator causes the login page URL to have a “next” CGI parameter with the original URL for the authorization request.
The problem is, the “next” parameter is not propagated when the template for the login page is instantiated. What I’d like is for the form “action” to contain the “next” parameter.
This page purports to offer a solution:
http://django-uni-form.readthedocs.org/en/latest/helpers.html
in the section “Manipulating a helper in a view”.
Following that example, I tried:
form = LoginForm()
redirect_url = request.GET.get('next')
if redirect_url is not None:
form.helper.form_action = reverse(login) + '?next=' +
redirect_url
But the source for the resulting page shows the original “action” for the form. The assignment to the form_action appears not to take effect. Indeed, if I don’t instantiate the template, but just return the form_action, it hasn’t changed:
return HttpResponse('action: ' + form.helper.form_action)
Color me puzzled.
The author of uni_forms clued me in here.
The problem is that the form helper was a property, so that the form_action was a fixed piece of that property.
By changing the property to a method that creates the helper, the form_action can be dynamically modified.
Everything works now, no Javascript hacks needed.