1

I have this models.py:

class dateEvent(models.Model):

    venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
    event = models.ForeignKey('Event', on_delete=models.CASCADE)
    start_date_time = models.DateTimeField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return "%s" % (self.start_date_time)

This is my context_processors.py:

def events(request):
    return
    {
        'next_events': dateEvent.objects.all(),
    }

which is registered in my settings.py:

        'OPTIONS': {
            'context_processors': [
                'nms_app.context_processors.events'

I'm getting

TypeError at /

'NoneType' object is not iterable
Exception Location:     /home/luca/python_ve/lib/python3.8/site-packages/django/template/context.py in bind_template, line 246
/usr/local/lib/python3.8/contextlib.py in __enter__

                return next(self.gen)

     …

▼ Local vars
Variable    Value
self    

<contextlib._GeneratorContextManager object at 0x7fe90a3e7a90>

when visiting any page of my website, and I can't understand why. Can someone help me finding the culprit? I can't see why dateEvent is NoneType; it is populated with valid data.

HBMCS
  • 686
  • 5
  • 25

2 Answers2

0

I can't believe it; it was 'only' an indentation problem. This code in my context_processors.py:

def events(request):
    return {
            'next_events': dateEvent.objects.all()
    }

works.

HBMCS
  • 686
  • 5
  • 25
-1

Where the error came from

My error came also from the context_processors.py I had an if statement like this.

    def myCP(request, *args, **kwargs):
        if request.user.is_authenticated:
            ...
            return {...}

After reading TypeError: ‘nonetype’ object is not callable the first one on this website I realised what the problem was.


The solution

The error came, because my function returned nothing when not logged in. To fix this I just added an else and returned something. For example return {'name': None}

context_processors.py

    def myCP(request, *args, **kwargs):
        if request.user.is_authenticated:
            ...
            return {...}
        
        # make sure that you always return something from a function
        else:
            return {...}

I also saw that this stack overflow question had an answer that explains it as well

'NoneType' object is not iterable in Django project


A solution I think is better

Instead of adding an else, you can just make a default return if all the if statements are False. This is useful when having 2 or more if statements that returns the same thing if they are False.

context_processors.py

    def myCP(request, *args, **kwargs):
        user = request.user
        if user.is_authenticated:
            ...
            
            if user.groups.filter(name='member').exists():
                return {...}
    
            
        # If it never reach any return above, it will then return this
        return {...}

AnonymousUser
  • 690
  • 7
  • 26