Search code examples
djangodjango-modelsdjango-generic-views

How to render context values in generic list and detail views


I have a base template file (shared.html) contains Header and Footer, using on every page. I have some dynamic values from database in shared.html (e.g: Phone number, address, logos) and showing perfectly on index view page, but not showing on any of the generic views page.

Please guide me how to do this to show all the dynamic values on every generic view page.

Index view:

def index(request):
    # Display all the Dynamic values form models
    num = TopBar.objects.get()
    addressISD = AddressIslamabad.objects.all()
    addressRWP = AddressRawalpindi.objects.all()
    alt = Logo.objects.all()
    YLP7Text = WhyLP7Text.objects.all()
    PMBG = BGimages.objects.all()
    lp7Features = LP7features.objects.all()
    locate = Locations.objects.all()
    events = Events.objects.all()
    memberLogo = LP7MembersLogo.objects.all()
    testi = LP7Testimonials.objects.all()
    promo = Promotions.objects.all()


    c = context = ({
    'topBarNumber': num,
    'addressISD': addressISD,
    'addressRWP': addressRWP,
    'altText': alt,
    'YLP7Text': YLP7Text,
    'BG': PMBG,
    'featuresLP7': lp7Features,
    'LC': locate,
    'evt': events,
    'memLogo': memberLogo,
    'testi': testi,
    'promo': promo

    })

    # Render the HTML template index.html
    return render(request, 'index.html', c )

Generic View:

# Display the detail and generic views
class EventListView(generic.ListView):
    model = Events


class EventDetailView(generic.DetailView):
    model = Events


class PromotionListView(generic.ListView):
    model = Promotions

class PromotionDetailView(generic.DetailView):
    model = Promotions


Solution

  • I think you can write a custom context processor for this. For example:

    def custom_context_processor(request):
        # Display all the Dynamic values form models
        num = TopBar.objects.get()
        addressISD = AddressIslamabad.objects.all()
        addressRWP = AddressRawalpindi.objects.all()
        alt = Logo.objects.all()
        YLP7Text = WhyLP7Text.objects.all()
        PMBG = BGimages.objects.all()
        lp7Features = LP7features.objects.all()
        locate = Locations.objects.all()
        events = Events.objects.all()
        memberLogo = LP7MembersLogo.objects.all()
        testi = LP7Testimonials.objects.all()
        promo = Promotions.objects.all()
    
        context = {
            'topBarNumber': num,
            'addressISD': addressISD,
            'addressRWP': addressRWP,
            'altText': alt,
            'YLP7Text': YLP7Text,
            'BG': PMBG,
            'featuresLP7': lp7Features,
            'LC': locate,
            'evt': events,
            'memLogo': memberLogo,
            'testi': testi,
            'promo': promo
        }
        return { 'common_content': context }
    

    And then add it to CONTEXT_PROCESSORS like this:

    TEMPLATES = [
        {
            # ...
            'OPTIONS': {
                'context_processors': [
                    'path.to.custom_context_processor',
                    # rest of the context processors
                ],
            },
        },
    ]
    

    And use this context processor in header and footer like this:

    {% for key, value in common_content.items %}
         {{ key }} {{ value }}
    {% endfor %}
    

    Finally, as it is making a lot of DB query, you can use template fragment caching to reduce DB hits.