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
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
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
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 %-)
> 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
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
--
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 %-)