Search code examples
djangoparentextends

Why passing arguments to parent template makes the child template not be able to use them


im making a django project for school website

I have a base.html that acts as parent template for the child templates which is the content of every page

The base.html includes a navbar with the school logo and a section on it titled "Units"

this is the code to render a lecturer page

views.py . .

def lecturer_home(request):
    user = request.user

        query for the user first name and full name
        query for the units that the user is teaching and their teaching 
        period in unit_list and period_display


        class_display = zip(unit_list, period_display)
        user_dict = {
        'f_name' : user.first_name,
        'fl_name' : user.first_name + ' ' + user.last_name,
        'class_display' : class_display,
        }
        return render(request, 'Lecturer/lecturerdashboard.html', user_dict)
    else:
        return HttpResponse('Unexpected error')

lecturerdashboard.html extends the base.html

I put less code for my views.py because I don't think I made any errors. What I want to confirm with you all is, the user_dict I passed in lecturerdashboard.html can also be used in the base.html, but confusingly I find that if a key and value is used in either one, the other one cannot use it. for example, I am able to display the units in the content section in the lecturerdashboard.html but when I used class_display in the base.html to show units as dropdown menu selection when the lecturer click on Units, the content section will not work because it doesnt understand class_display. sorry if the question is confusing in summary, the parent and child understands the argument passed by the view but if a key, value is used in parent, the child does not understand it i just want to confirm, is this true? thank you


Solution

  • This doesn't really have anything to do with templates. zip is an iterator. Once you iterate through it, it is exhausted, and can't be used again. If you want to iterate it multiple times, call list on it:

     class_display = list(zip(unit_list, period_display))