0

I am trying to update my user account info through a form. I have the form ok which just displays a password/email field and cleans the email field. I am having a problem with my views. This is the error I get: Manager isn't accessible via User instances. This is my views: my_account function.

def my_account(request):
    user = request.user
    if request.method == 'POST':
        form = MyAccountForm(request.POST)
        if form.is_valid():
            user = user.objects.get(username=username),
            password = user.set_password('password2'),
            email = forms.cleaned_data['email']
            user.save()
            return HttpResponseRedirect('/')
    else:
        form = MyAccountForm()
    variables = RequestContext(request, {
        'form': form,
    })
    return render_to_response(
        'my_account.html',
        variables
    )
Megan
  • 622
  • 1
  • 10
  • 16
  • this is, however, an exact duplicate of: http://stackoverflow.com/questions/3874187/manager-isnt-accessible-via-model-instances. Please try simply searching your error message before posting a new question. – Colleen Dec 11 '12 at 17:21
  • I did and obviously that wasn't my only issue. I always look up my own errors first but sometimes thats not always helpful. – Megan Dec 11 '12 at 17:47

1 Answers1

2

where you have

user.objects.get

you want

User.objects.get

objects is the manager referred to in the error message, and user is the instance referred to (an instance of User, the actual class)

Colleen
  • 23,899
  • 12
  • 45
  • 75
  • ok did that now it says username is not defined so i added username to the paramaters next to request and to my variables with forms but now i get the error: my_account() takes exactly 2 arguments (1 given) – Megan Dec 11 '12 at 17:26
  • Wow, there's a whole host of problems here. Sounds like you need to 1: read your error messages, 2: learn more about Django, 3: proofread your code. Let's tackle these: 1. "`my_account() takes exactly 2 arguments (1 given)`"= you changed `my_account` so it takes 2 params, but you're still only giving it 1 (presumably the request). 2: As far as I'm aware, all the data in Django forms is serialized into the `POST` field in the `request`; variables are not passed individually. To access `username` you should not have added it as a new param, but extracted it from the request. – Colleen Dec 11 '12 at 17:30
  • 3: look at your code. You *haven't* defined `username` anywhere (ties back into problem #1-- not actually reading your error messages). You just use it in the `get` call. You need to actually extract it from the request before you can pass it to anything else! – Colleen Dec 11 '12 at 17:31
  • I was trying to extract it from the request for instance user = request.user(user = User.username), I know that is not correct but I could not figure out how to call it. The reason I added it as a parameter was because I did it in a previous view page- user_page and it worked correctly so I wanted to see if that would help. I am still very new to this so I'm sorry I don't fully understand everything yet. Also, I do read my error messages but sometimes I do not fully understand the issue. I knew it was having a problem with username and that I hadn't called it anywhere I just didn't know where. – Megan Dec 11 '12 at 17:45
  • as an example, look at how you're extracting `email`. That's probably the way you want to extract `username`. Though now that I'm rereading your code, why are you even doing this? You already have `user` from when you did `user = request.user`. Also, this snippet `user = request.user(user = User.username)` is wrong for a lot of reasons: 1. in `request.user`, `user` is a property, not a function, 2: you're comparing `user` to `username` in that clause, 3: as I said above, `User` is a class name, not an instance name, and as such will have no `.username` property. – Colleen Dec 11 '12 at 17:48
  • At any rate, I think since you already have `user`, you should just delete the line where you're reassigning it and be good to go. – Colleen Dec 11 '12 at 17:52
  • My professor wants us to call the username so how would I extract that from request.user? – Megan Dec 11 '12 at 17:55
  • What does "call the username" mean? And user is an instance of User, so it has all of the properties of any other instance of User... like username... – Colleen Dec 11 '12 at 17:58