i made context_processors to pass data (objects of 'League' class) to 'base.html' but when i clicked on one of anchor tags (that contain league object) it produced " TypeError 'League' object is not iterable "

i tried to pass data to 'base.html' .... to provide pass this data to all views 'context_processors.py'

from .models import League
def add_to_base(request):
    return {
        'league' : League.objects.all()
    }
'base.html'
    <div id="league-container">

      {% for l in   league   %}
          <a href="{% url 'core:openleague' l.pk %}"><h2>{{ l.leagues }}</h2></a>
      {% endfor %}
    </div>
  this is view function
    def openleague(request, pk):
       league = League.objects.get(pk=pk)
       news = league.news.filter(publication_date__lte=timezone.now()).order_by('-publication_date')[:20]
       return render(request, 'core/openleague.html', {'league':league,'news':news})

and this is traceback
Support
TypeError at /11/openleague/ by Django
New
About
Yours
API
Help
4.2 KB, Python console session
    Unwrap Raw text Duplicate


Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/11/openleague/

Django Version: 2.1.4
Python Version: 3.5.2
Installed Applications:
['core.apps.CoreConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_social_share']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/omar/my-django/arena/templates/base.html, error at line 36
   'League' object is not iterable
   26 :     
   27 :        
   28 :        
   29 :        <li><a href="{% url 'core:home' %}" >الرئيسية</a></li>
   30 :      </ul>
   31 :      {% if user.is_authenticated %}
   32 :        <h4>logged in as {{ user.username }}</h4>
   33 :      {% endif %}
   34 :      <div id="league-container">
   35 : 
   36 :        {% for l in  league  %} 
   37 :           <a href="{% url 'core:openleague' l.pk %}"><h2>{{ l.leagues }}</h2></a>
   38 :       {% endfor %}
   39 :     </div>
   40 :     </nav>
   41 : 
   42 :           <main>
   43 :           {% block content %}
   44 :         {% endblock %}
   45 :       </main>
   46 : 


Traceback:

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/omar/my-django/arena/core/views.py" in openleague
  31.     return render(request, 'core/openleague.html', {'league':league,'news':news})

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/home/omar/my-django/env/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  165.                 values = list(values)

Exception Type: TypeError at /11/openleague/
Exception Value: 'League' object is not iterable

1 Answers

1
Ben Boyer On Best Solutions

Create a context_processor.py in your app

from .models import League
def add_to_base(request):
    return {
        'league_from_processor' : League.objects.all()
    }

In the project setting add settings.py add this in TEMPLATES => OPTIONS => context_processors list

    "app_name.context_processor.add_to_base",

'base.html'

<div id="league-container">

  {% for l in   league_from_processor   %}
      <a href="{% url 'core:openleague' l.pk %}"><h2>{{ l.leagues }}</h2></a>
  {% endfor %}
</div>

More help here: https://www.youtube.com/watch?v=7nwFr4FrEP4