Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top