7

When I try to submit, I get a TypeError:

int() argument must be a string or a number, not 'SimpleLazyObject'

My views.py:

def bookmark_save_page(request):
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            # Create or get link.
            link, dummy = Link.objects.get_or_create(
                url=form.cleaned_data['url']
                )
            # Create or get bookmarks.
            bookmark, created = Bookmark.objects.get_or_create(
                user = request.user,
                link = link
                )
            # Update bookmark title.
            bookmark.title = form.cleaned_data['title']
            # If the bookmark is being updated, clear old tag list.
            if not created:
                bookmark.tag_set.clear()
            # Create new tag list.
            tag_names = form.cleaned_data['tags'].split()
            for tag_name in tag_names:
                tag, dummy = Tag.objects.get_or_create(name=tag_name)
                bookmark.tag_set.add(tag)
            # Save bookmark to database.
            bookmark.save()
            return HttpResponseRedirect(
                '/user/%s/' %request.user.username
                )
    else:
        form = BookmarkSaveForm()

    variables = RequestContext(request, {'form': form})
    return render_to_response('bookmark_save.html',variables)

Please guide me. Thank you.

karthikr
  • 97,368
  • 26
  • 197
  • 188
Tchec
  • 97
  • 1
  • 1
  • 8
  • possible duplicate of [int() argument must be a string or a number, not 'SimpleLazyObject'](http://stackoverflow.com/questions/15878860/int-argument-must-be-a-string-or-a-number-not-simplelazyobject) – Aamir Rind Jul 12 '13 at 20:20

2 Answers2

14

request.user, by default is of type SimpleLazyObject. To resolve it,

bookmark, created = Bookmark.objects.get_or_create(
            user = request.user,
            link = link
            )

should be

bookmark, created = Bookmark.objects.get_or_create(
            user = request.user.id,
            link = link
            )

If this does not resolve it, make sure you are logged in.

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 1
    Ok, I did, as u have writen here. But now I am getting another error.: "Cannot assign None: "Bookmark.user" does not allow null values." – Tchec Jul 12 '13 at 20:19
  • 2
    looks like you are not logged in . are you ? use `@login_required` decorator – karthikr Jul 12 '13 at 20:20
  • 2
    Okay, sorry i was not logged in. But now when i am, I am getting this error: `Cannot assign "3": "Bookmark.user" must be a "User" instance` – Tchec Jul 12 '13 at 20:33
  • Really ? That is wierd. Now, try `user = request.user` without the `id` . Do you get the same issue? – karthikr Jul 12 '13 at 20:36
  • Okay... That did it. Thank you! But can you please tell me what was the problem here. Was it just because authentication? And one more thing, when the tried to click on the Links or the bookmarks, they are not redirecting to its respective URL, just coming back to its user page. I am sorry, if that was too much. But thank you again! – Tchec Jul 12 '13 at 20:47
  • `request.user` by default is not a `user` but `SimpleLazyObject` type. At the time of consuming the variable, it evaluates `request.user` to `user` instance if user is logged in. If user is not logged in, it evaluates to `AnonymousUser` object. Hence the issue. For the redirect URLs, most likely `bookmark.get_absolute_url()` is returning `None` – karthikr Jul 12 '13 at 20:51
  • Thank you Karthik. I didn't get the last part about URL, but i think if i ask more, i will be of the topic. Thank you so much though! – Tchec Jul 12 '13 at 21:08
  • Create a new question. You should get a good response for that. And do mark this answer as accepted if it was useful – karthikr Jul 12 '13 at 21:09
1

There is another option, may be this solution will help you fix it.

from django.contrib import auth

bookmark, created = Bookmark.objects.get_or_create(
        user = auth.get_user(request),
        link = link
        )
Arun V Jose
  • 3,321
  • 2
  • 24
  • 24