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

前端 未结 5 752
南方客
南方客 2020-12-13 19:33

I got, following Error messages,

TypeError at /save/ int() argument must be a string or a number, not \'SimpleLazyObject\'

Whi

5条回答
  •  无人及你
    2020-12-13 19:55

    Most likely the user who is loading the page is not authenticated. Therefor the error is thrown. If you want to save a request.user reference to the database, you obviously have to ensure that only authenticated users are able to call the function.

    In your case there are two possibilities - add the "@login_required" decorator to the function or check if the user is authenticated inside the code. Here are the snippets:

    With Decorator:

    from django.contrib.auth.decorators import login_required
    
    @login_required
    def bookmark_save_page(request):
        if request.method == 'POST':
            form = BookmarkSaveForm(request.POST)
            if form.is_valid():
                # Do something
    

    OR - checking inside the code if the user is authenticated:

    def bookmark_save_page(request):
        if request.method == 'POST' and request.user.is_authenticated():
            form = BookmarkSaveForm(request.POST)
            if form.is_valid():
                # Do something
    

提交回复
热议问题