Home > Enterprise >  Django: how to extends a view to another view without copy and paste?
Django: how to extends a view to another view without copy and paste?

Time:04-27

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.

CodePudding user response:

I think you can use a custom context processor to add all this data to your templates. Example:

import models
def index(request):
    """Custom context processor."""
    designcatlist = DesignCategory.objects.all()
    prglangcatlist = ProgrammingLanguagesCategory.objects.all()
    howtocatlist = HowToCategory.objects.all()
    
    return {
        'designcat': designcatlist,
        'prglangcat': prglangcatlist,
        'howtocat': howtocatlist,
    }

After created it or them (because you can create just one with all data or several: one for each function) only have to add it to TEMPLATES.OPTIONS.context_processors. The documentation explains that can live anyone, so you define if can put in an app our in own package. So:

TEMPLATES = [
   {
       ...,
       OPTIONS: {
         'context_processors': [
              "other.django.processors",
              "package.processor.index",
          ]
       }
}]

Now all the values returned by processors are available in the templates. So in your templates only use the keys of dict returned it in processor to call it as if they were in normal context: {{howtocat}}.

CodePudding user response:

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

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

  • Related