我有一个基本模板文件(shared.html),该文件包含在每个页面上使用的页眉和页脚。我在shared.html中从数据库中获得了一些动态值(例如:电话号码,地址,徽标),并在索引视图页面上完美显示,但在任何通用视图页面上却没有显示。
请指导我如何执行此操作,以在每个常规视图页面上显示所有动态值。
索引视图:
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
答案 0 :(得分:0)
在您的通用视图中,
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 %}
答案 1 :(得分:0)
在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中阅读有关此内容的更多信息,这些示例非常详尽。
答案 2 :(得分:0)
我认为您可以为此写一个custom context processor
。例如:
apply plugin: 'com.android.application'
apply plugin: 'maven'
android {
compileSdkVersion 29
...
然后像这样将其添加到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
],
},
},
]
最后,由于它进行了大量数据库查询,因此您可以使用template fragment caching
来减少数据库命中率。