views:

131

answers:

2

After being dissatisfied with the existing solutions I wrote an OpenId provider for Django.

If now somebody wants to authenticate himself somewhere as http://tejp.de/users/abc/ and needs to login for that, I want to display the login form with the username preset to "abc". The standard functions like redirect_to_login don't seem to provide any parameters for this and I also don't see how I could preset that value when redirecting to the login view in django.contrib.auth.views.login manually. Also there seems to be no easy way to get an additional parameter value through to the template, so that I could maybe insert the preset value there.

Is there a way to automatically fill in a username in the login form? If possible I'd like to use the normal login view for this, not copy&paste all the login code and edit in the necessary changes.

A: 

Some of ways to solve the above problem are:

  1. Write your own authentication form and prefill the username before displaying.

  2. Use javascript to strip a username from url and put it down in the form.

Rama Vadakattu
A: 

Remember, django is all python, and the source is your friend.

Here's the original source for redirect_to_login.

def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    "Redirects the user to the login page, passing the given 'next' page"
    if not login_url:
        login_url = settings.LOGIN_URL 
    return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next)))

It looks like you could even just override login_url with a link to a URL that takes the appropriate parameters. Something like

return redirect_to_login(login_url=reverse('my_login_view', [], {'username': username}))

You don't need a new form, you just need to prepopulate the existing login form.

jcdyer