代码之家  ›  专栏  ›  技术社区  ›  Destiny Franks

“tuple”对象没有“is_preview”属性

  •  1
  • Destiny Franks  · 技术社区  · 2 年前

    我正在做一个项目,用户可以创建一个视频,并检查是否可以在购买前预览特定内容,直到现在一切都很好,我得到了这个 'tuple' object has no attribute 'is_preview' 我真的不知道这个错误是从哪里来的。

    这也是回溯指向的地方 回溯

    Traceback (most recent call last):
      File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
        response = get_response(request)
      File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
        return view_func(request, *args, **kwargs)
      File "C:\Users\Destiny\Desktop\DexxaEd\dnextedprj\course\views.py", line 122, in course_details
        if(video.is_preview is False):
    AttributeError: 'tuple' object has no attribute 'is_preview'
    [01/May/2022 20:08:43] "GET /course/et-ea-velit-ad-veritatis-explicabo HTTP/1.1" 500 73905
    

    意见。py线122

    
        if serial_number is None:
            serial_number = 1
    
        
        # if Video.objects.all().exists
        video = Video.objects.get_or_create(serial_number=serial_number, course=course)
        video_count = Video.objects.get(serial_number=serial_number, course=course)
    
        if(video.is_preview is False):
    
            if request.user.is_authenticated is False:
                return redirect("course:sign-in")
            else:
                user = request.user
                try:
                    user_course = UserCourse.objects.get(user=user, course=course)
                except:
                    return redirect("course:check-out", slug=course.slug)
    
    

    类型py

    CourseVideosFormset = inlineformset_factory(Course, Video, fields=('title','serial_number', 'video_id', 'is_preview'))
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   willeM_ Van Onsem    2 年前

    .get_or_create(…)  [Django-doc] 返回一个2元组,其中创建/检索的对象为第一项,布尔值为 True 以防该对象被创建为第二个。因此,您可以用以下方法打开这些文件:

    video, __ = Video.objects.get_or_create(serial_number=serial_number,