如何获得所有模板的相同信息?

时间:2018-10-06 08:52:11

标签: html django jinja2 django-2.1

因此在Django中,我有一个基本模板,其中包含一些联系方式。但是我生成的每个视图都必须有一行。

contact = Contact.objects.first()

然后我必须将该对象添加到随模板一起加载的字典中。

更好的应对方法是什么?我发现很难相信我正在以正确的方式进行操作。

例如views.py

from django.shortcuts import render
from services.models import Service, ServicesDetail
from .models import Feature, CompanyDetail, TeamMember, TeamDetail, Banner
from contact.models import ContactDetail
import json

# Create your views here.
def home(request):
    services = Service.objects
    try:
        overview = ServicesDetail.objects.first()
    except ServicesDetail.DoesNotExist:
        overview = ''
    try:
        company = CompanyDetail.objects.first()
    except CompanyDetail.DoesNotExist:
        company = ''

    features = Feature.objects

    contact_details = ContactDetail.objects.first()

    banners = Banner.objects
    return render(request, 'home.html', {'overview': overview,
                                         'services': services,
                                         'company': company,
                                         'features': features,
                                         'contact_detail': contact_details,
                                         'banners': banners})

def company(request):
    services = Service.objects
    try:
        company = CompanyDetail.objects.first()
    except CompanyDetail.DoesNotExist:
        company = ''

    features = Feature.objects
    contact_details = ContactDetail.objects.first()
    return render(request, 'company.html', {'services': services,
                                            'company': company,
                                            'features': features,
                                            'contact_detail': contact_details,})

def team(request):
    services = Service.objects
    members = TeamMember.objects
    try:
        teampage = TeamDetail.objects.first()
    except TeamDetail.DoesNotExist:
        teampage = ''
    contact_details = ContactDetail.objects.first()
    return render(request, 'team.html', {'services': services,
                                         'members': members,
                                         'teampage': teampage,
                                         'contact_detail': contact_details,})

2 个答案:

答案 0 :(得分:1)

您不需要在每个视图中都这样做,只需编写自定义context processor

$true

并将其添加到def contact_details(request): return {'contact_detail': contact_details = ContactDetail.objects.first()} 设置中:

TEMPLATES

答案 1 :(得分:1)

您可以切换到class base template views并编写您的自定义基类

class MyBaseTemplateView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['contact_details'] = ContactDetail.objects.first()
        return context


class MyActualView(MyBaseTemplateView):
    template_name = 'company.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # set other specific context values for this view here
        return context

像这样将这些视图添加到您的urls.py

urlpatterns = [
    path('', MyActualView.as_view(), name='myactualview'),
]