Search code examples
djangodjango-modelsdjango-context

'NoneType' object is not iterable - issue with context in Django


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.


Solution

  • 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.