Login Right After Creating a New User

21 views
Skip to first unread message

super.sg...@gmail.com

unread,
Jun 7, 2007, 12:03:04 AM6/7/07
to Django users
Hi Everyone,
I'm trying out django and it's been really easy getting the auth
library up and running. I hit an issue where I want to login a new
user right after they've registered and take them to their profile
page.

I tried calling the auth.login method but I got an error saying that
the new_user object doesn't have the attribute error. I guess this is
because the session and auth middlewares didn't get to alter new_user.

Does django provide a quick way of resolving this?

Thanks a lot,
Cristian

My code:

def register(request):
form = UserCreationForm()
if request.method == 'POST':
data = request.POST.copy()
errors = form.get_validation_errors(data)
if not errors:
new_user = form.save(data)
auth.login(request, new_user)
return HttpResponseRedirect('/accounts/profile')
else:
data, errors = {}, {}
return render_to_response('register.html',
{'form': forms.FormWrapper(form, data,
errors)})


My Error:

AttributeError at /register/
'User' object has no attribute 'backend'
Request Method: POST
Exception Type: AttributeError
Exception Value: 'User' object has no attribute 'backend'
Exception Location: C:\Python25\lib\site-packages\django\contrib\auth
\__init__.py in login, line 55

Russell Keith-Magee

unread,
Jun 7, 2007, 1:04:06 AM6/7/07
to django...@googlegroups.com
On 6/7/07, super.sg...@gmail.com <super.sg...@gmail.com> wrote:
>
> Hi Everyone,
> I'm trying out django and it's been really easy getting the auth
> library up and running. I hit an issue where I want to login a new
> user right after they've registered and take them to their profile
> page.
>
> I tried calling the auth.login method but I got an error saying that
> the new_user object doesn't have the attribute error. I guess this is
> because the session and auth middlewares didn't get to alter new_user.
>
> Does django provide a quick way of resolving this?

Yes. The 'backend' attribute that is being complained about is an
attribute that is decorated onto the user object by the authenticate()
method (django.contrib.auth.authenticate()). If you call authenticate,
providing the login credentials, you will get a 'decorated' user that
can then be used in the login method.

Yours,
Russ Magee

Vincent Nijs

unread,
Jun 7, 2007, 1:19:14 AM6/7/07
to django...@googlegroups.com
Hi,

I am trying to create a group so that I can then add users to that group. I
tried the following which is very similar to how you create users.

from django.contrib.auth.models import Group
g = Group.objects.create('section81')

However this give the following error:

exceptions.TypeErro Traceback (most recent call last)

TypeError: create() takes exactly 1 argument (2 given)

Any ideas?

Thanks,

Vincent

--
Vincent R. Nijs
Assistant Professor of Marketing
Kellogg School of Management, Northwestern University
2001 Sheridan Road, Evanston, IL 60208-2001
Phone: +1-847-491-4574 Fax: +1-847-491-2498
E-mail: v-n...@kellogg.northwestern.edu
Skype: vincentnijs

Russell Keith-Magee

unread,
Jun 7, 2007, 1:55:41 AM6/7/07
to django...@googlegroups.com
On 6/7/07, Vincent Nijs <v-n...@kellogg.northwestern.edu> wrote:
>
> TypeError: create() takes exactly 1 argument (2 given)
>
> Any ideas?

Calling


g = Group.objects.create("section81")

is the same as calling

Group("section81")

This yeilds an error because there is no way of knowing which model
field the string "section81" should be applied to.

If you make this a keyword argument:

g = Group.objects.create(name="section81")

the problem should resolve itself.

Yours,
Russ Magee %-)

super.sg...@gmail.com

unread,
Jun 7, 2007, 3:59:08 AM6/7/07
to Django users
On Jun 6, 10:04 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

> Yes. The 'backend' attribute that is being complained about is an
> attribute that is decorated onto the user object by the authenticate()
> method (django.contrib.auth.authenticate()). If you call authenticate,
> providing the login credentials, you will get a 'decorated' user that
> can then be used in the login method.
>
> Yours,
> Russ Magee

Thanks a lot! Worked like a charm.

- Cristian

Vincent Nijs

unread,
Jun 7, 2007, 11:03:46 AM6/7/07
to django...@googlegroups.com
Thanks for the help Russ:

That worked. Next problem :) I want to add users to a group.

g = Group.objects.get(name='section81')

gives errors.

If I could get that to work I'd want to add a user to that group:

u = User.objects.get(username='doe')

This also give errors.

Finally:

u.groups.add(g)

Which doesn't work because the previous 2 didn't :)

Sorry to ask multiple questions about this but I can't seem to find any
clear documentation on this.

Vincent

--

Russell Keith-Magee

unread,
Jun 7, 2007, 8:19:11 PM6/7/07
to django...@googlegroups.com
On 6/7/07, Vincent Nijs <v-n...@kellogg.northwestern.edu> wrote:
>
> Thanks for the help Russ:
>
> That worked. Next problem :) I want to add users to a group.
>
> g = Group.objects.get(name='section81')
>
> gives errors.

I'm guessing the errors you are getting are telling you that various
attributes can't be NULL. Group has a number of attributes other than
'name' - they all need to be specified.

However, it's difficult to say for certain. You could help me out here
by telling me _what_ errors you are receiving. It's difficult to offer
advice when all you're telling me is "it's broken".

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages