Django Middleware subscription check


I have a function that runs as middleware, but for some reason its never moving to the if statement even when evaluating to True

class SubscriptionCheckMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Enhanced logging for debugging
        print("Middleware called")
        if request.user.is_authenticated:
                subscription = request.user.profile.subscription
                print('User authenticated:', request.user.is_authenticated)
                print('User subscription:', subscription, type(subscription))
                no_licence_path = reverse('no_licence')
                current_path = request.path
                print('Current path:', current_path)
                print('No licence path:', no_licence_path)
                if subscription is None:
                    print('Subscription is None, redirecting to no_licence page')
                    if current_path != no_licence_path:
                        return redirect('no_licence')
                        print('Already on no_licence path, no redirect to avoid loop')
            except AttributeError as e:
                # Handle cases where the profile or subscription is not found
                print('AttributeError encountered:', str(e))
                return redirect('no_licence')
        response = self.get_response(request)
        return response

The idea being that it will capture every request to make sure the user has an active subscription of Basic, Standard or Pro and if this is not True it will redirect to a no_licence page

I’ve added some debugging print statements and all looks good, but just never reaches the if subscription is None:

Im thinking maybe its something to do with the middleware ordering?

    #'django.middleware.cache.UpdateCacheMiddleware', # Cache middleware

    # Cache middleware
    #'django.middleware.cache.FetchFromCacheMiddleware'  # Cache middleware


Not sure where I’m going wrong, or if there is a better way to do things?



What are you getting as the output from these prints?

What does your profile model look like?

Hi Ken

Middleware called
User authenticated: True
User subscription: Pro <class 'str'>
Current path: /accounts/no_licence
No licence path: /accounts/no_licence
Subscription: Pro
Subscription is None, redirecting to no_licence page
Already on no_licence path, no redirect to avoid loop

Its printing out that the subscription is Pro but still moving to Subscription is None i dont get it.

Any clues?

This is a puzzler.

Are you running this in your development environment with runserver (or runserver_plus)? If so, can you copy a larger block of the console output to include the requests being made so that I can see this in context.

I’m sorry I have to go on this fishing expedition, I know I’m missing something, but I don’t know what yet. (I don’t have any specific ideas, I’m just trying to gather information to figure out what I need to ask next.)

Looks like the output does not match the code provided in your first post.

The log “Subscription: Pro” between “No licence path: …” and “Subscription is None” messages does not correspond to any print() statement.

Are you sure the code showed in your post is exactly what is running on your server ?