如何在通用列表和详细视图中呈现上下文值

How to render context values in generic list and detail views

我有一个基本模板文件 (shared.html) 包含页眉和页脚,在每个页面上使用。我在 shared.html 中有一些来自数据库的动态值(例如:Phone 号码、地址、徽标)并在索引视图页面上完美显示,但未在任何通用视图页面上显示。

请指导我如何在每个通用视图页面上显示所有动态值。

索引视图:

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 )

通用视图:

# 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

在您的一般观点中,

class PromotionDetailView(generic.DetailView):
    model = Promotions
    template_name = 'promotions/promotions.html' #<app_name>/<model>_<viewtype>.html
    context_object_name = 'Promotions'

在您的 promotions.html 中执行此操作

{% extends 'blog/base.html'%}
{% block content %}
    {% for promotion in Promotions%}
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ promotion.logos.url }}">
        <div class="media-body">
            <div class="article-metadata">
                <a class="mr-2" href="{% url 'user-post' promotion.Addresses %}">{{ promotion.Name }}</a>
                <small class="text-muted">{{ promotion.Date_posted|date:"d F, Y" }}</small>
            </div>
            <p class="article-content">{{ promotion.Phone_Number}}</p>
        </div>
    </article>
    {% endfor %}
{% endblock content %}

GenericViews 中,您可以覆盖 get_context_data 方法,以便在模板中设置您需要的 context

你可以这样做:

class EventListView(generic.ListView):
    model = Events
    # We could explicitly tell the view which template 
    # to use by adding a template_name attribute to the view, 
    # but in the absence of an explicit template Django will infer one
    # from the object’s name
    # template_name = 'path/template.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Update context with data needed
        context['foo'] = 'bar'
        context['foo_2'] = 'bar_2'
        return context

在您的模板中,您将能够访问 context 作为:

{% for object in object_list %}
    {{ object.foo }}
    {{ object.foo_2 }}
{% endfor %} 

对于 DetailViews,您可以覆盖相同的 get_context_data 方法,但不需要在模板中循环。

您可以在 documentation 中阅读更多相关信息,这些示例非常详尽。

我想你可以为此写一个custom context processor。例如:

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 }

然后像这样将其添加到 CONTEXT_PROCESSORS

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

并像这样在页眉和页脚中使用此上下文处理器:

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

最后,由于它进行了大量的数据库查询,您可以使用template fragment caching来减少数据库命中。