'Django: how to extends a view to another view without copy and paste?

i have a view that shows my navigation bar, i am adding the navgigation bar dynamically from the database, but th problem is that i have to add that same piece of code to all my views for the navigation text to show up and that is making my code so length, is there a way to automatically extends the peice of code that is getting my navigation to all the views, so when i go to another page like the about page or contact, or profile page i can still see the navigation bar. Now only when i see the navigation bar is when i go to the index.html

views.py

def index(request):
    designcatlist = DesignCategory.objects.all()
    prglangcatlist = ProgrammingLanguagesCategory.objects.all()
    howtocatlist = HowToCategory.objects.all()
    context = { ... }

def about_page(request):
    designcatlist = DesignCategory.objects.all()
    prglangcatlist = ProgrammingLanguagesCategory.objects.all()
    howtocatlist = HowToCategory.objects.all()
    context = { ... }

def contact_page(request):
    designcatlist = DesignCategory.objects.all()
    prglangcatlist = ProgrammingLanguagesCategory.objects.all()
    howtocatlist = HowToCategory.objects.all()
    context = { ... }

def profile_page(request):
    designcatlist = DesignCategory.objects.all()
    prglangcatlist = ProgrammingLanguagesCategory.objects.all()
    howtocatlist = HowToCategory.objects.all()
    context = { ... }

NOTE as you can see in the code above, i need to add the same piece of code for the naviation to show up on those pages.



Solution 1:[1]

If you use class based views they can inherit from one another.

https://docs.djangoproject.com/en/4.0/topics/class-based-views/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maciek.glowka