[Answered ]-Django: Create a dynamic sidebar template and use it in other templates

1πŸ‘

βœ…

To achieve this you need to pass the same context in your all views. A simple demonstration of your views would be as follows:

views.py

# Since these context will be common to all views it would be written outside any view function
def get_common_queryset():
    usage_queryset = Usage.objects.all()
    sub_usage_queryset = SubUsage.objects.all()
    main_model_queryset = MainModel.objects.all()
    pump_type_queryset = PumpType.objects.all()
    queryset_dictionary = {
        "usage_queryset": usage_queryset,
        "sub_usage_queryset": sub_usage_queryset,
        "main_model_queryset": main_model_queryset,
        "pump_type_queryset": pump_type_queryset,
    }
    return queryset_dictionary



# and in every other views
def products_usage_main(request):
    ...
    context_of_view = {
    ...
    }
    common_context = get_common_queryset()
    context = {**context_of_view, **common_context} # dictionary expansion
    return render(request, "template_name.html", context)

πŸ‘€ilyasbbu

Leave a comment