/ memberships / payal ocal变量'course'在分配之前引用的UnboundLocalError

问题描述 投票:0回答:1
from django.shortcuts import render
from django.views.generic import ListView, DetailView,View
from memberships.models import UserMembership
from .models import Course, Lesson

# Create your views here.
class CourseListView(ListView):
    model = Course

class CourseDetailView(DetailView):
    model = Course

class LessonDetailView(View):
    def get(self,request,course_slug,lesson_slug, *args,**kwargs):

        course_qs = Course.objects.filter(slug=course_slug)
        if course_qs.exists():
            course = course_qs.first()

        lesson_qs = course.lessons.filter(slug=lesson_slug)
        if lesson_qs.exists():
            lesson = lesson_qs.first()

        user_membership = UserMembership.objects.filter(user=request.user).first()
        user_membership_type = user_membership.membership.membership_type

        course_allowed_mem_types = course.allowed_memberships.all()

        context = {
            'object' : None
        }
        if  course_allowed_mem_types.filter(membership_type=user_membership_type).exists():
            context = {'object': lesson}
        return render(request, "courses/lesson_detail.html",context)
Internal Server Error: /memberships/payment
Traceback (most recent call last):
  File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\ASUS\Envs\web\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\ASUS\Envs\web\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\ASUS\myproject\cpx\courses\views.py", line 20, in get
    lesson_qs = course.objects.filter(slug=lesson_slug)
UnboundLocalError: local variable 'course' referenced before assignment
python django python-3.7
1个回答
0
投票
  • from django.conf import settings
    from django.contrib import messages
    from django.http import HttpResponseRedirect
    from django.shortcuts import render
    from django.urls import reverse
    from django.views.generic import ListView
    from .models import Membership , UserMembership, Subscription
    import stripe
    # Create your views here.
    
    def get_user_membership(request):
       user_membership_qs = UserMembership.objects.filter(user = request.user)
       if user_membership_qs.exists():
           return user_membership_qs.first()
    
       return None
    
    def get_user_subscription(request):
       user_subscription_qs =  Subscription.objects.filter(
           user_membership = get_user_membership(request))
       if user_subscription_qs.exists():
           user_subscription = user_subscription_qs.first()
           return user_subscription
       return None 
    
    def get_selected_membership(request):
       membership_type = request.session['selected_membership_type']   
       selected_membership_qs = Membership.objects.filter(
               membership_type = selected_membership_type)
       if selected_membership_qs.exists():
           return selected_membership_qs.first()
       return None 
    
    class MembershipSelectView(ListView):
       model = Membership
       def get_context_data(self, *args, **kwargs):
           context = super().get_context_data(**kwargs)
           current_membership = get_user_membership(self.request)
           context['current_membership'] = str(current_membership.membership)
           return context
    
       def post(self, request, **kwargs):
           selected_membership_type = request.POST.get('membership_type')  
           user_membership = get_user_membership(request)
           user_subscription = get_user_subscription(request)      
           selected_membership_qs =Membership.objects.filter(
               membership_type = selected_membership_type) 
           if selected_membership_qs.exists():
               selected_membership = selected_membership_qs.first()
    
               #validation
    
    
           if user_membership.membership == selected_membership:
               if user_subscription != None:
                   messages.info(request, "you already have this membership. your \
                       next payment is due {}".format('get this value from stripe'))
                   return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    
    
           request.session['selected_membership_type'] = selected_membership.membership_type       
           return HttpResponseRedirect(reverse('memberships:payment'))
    
    def PaymentView(request):
       user_membership = get_user_membership(request)
       selected_membership = get_selected_membership(request)
       publishKey = settings.STRIPE_PUBLISHABLE_KEY
    
       context = {
           'publishKey': publishKey,
           'selected_membership': selected_membership
       }
       return render(request, "memberships/membership_payment.html", context)
    
© www.soinside.com 2019 - 2024. All rights reserved.