0
def itemconfirmation(request, pk):
    item = Food_item.objects.get(id=pk)
    userobj = request.user
    user = UserProfile.objects.get(user=userobj)
    if request.method == 'POST':
        count_form = CountForm(data=request.POST)
        if count_form.is_valid():
            countform = count_form.save(commit=False)
            countform.useradno = user.adno
            countform.itemid = item.id
            countform.save()
    c = RequestContext(request, {
                        'item': item, 'count_form': count_form
               })
    return render_to_response('itemconfirmation.html', context_instance=c)

I have a view defined like this. I'm getting error in making the user object extended to UserProfile and cannot user the user.id

1
  • What error? Please show the traceback. Commented Mar 9, 2014 at 14:52

1 Answer 1

1

Is the request.user authenticated? is it an AnonymousUser?

UserProfile.objects.get() method is not for creating an object but to get it from the database. if it doesn't exist an exception will be raised. use UserProfile.objects.create(..) with the initial data you may need for it.

hope this helps!

== edit ==

also, note that you are referring count_form in the RequestContext even when it wasn't initialized in case that the request.method was not "POST" (i.e. "GET")

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The requested user was anonymous. Fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.