英文:
i am trying to pass the title form model to a template for nav dropdown but it is working in main html page but not showing others
问题
我正在尝试创建一个导航下拉菜单,其中包含模型标题,但它只在主HTML页面上显示下拉菜单,而在其他页面上不显示,我还尝试创建一个带有标题的头部HTML模板,但标题也不显示,那么我如何将对象传递给多个模板?
我的模板下拉菜单:
<li class="menu-has-children"><a href="#services">所有服务</a>
<ul style="display: inline;">
{% for detailinfo in detail.all %}
<li><a href="{% url 'details' services.slug %}">{{ detailinfo.title }}</a></li>
{% endfor %}
</ul>
</li>
我的视图:
def details(request, services_slug):
q = services.objects.filter(slug=services_slug)
if q.exists():
q = q.first()
else:
return HttpResponse("<h1>页面未找到</h1>")
detail = {'detail': q}
return render(request, 'detail.html', detail,)
我想在详情页面中添加这些对象。
def homepage(request):
aboutinfo = aboutpage.objects
servicesinfo = services.objects
programinfo = prgm.objects
return render(request, 'index.html', {'aboutinfo': aboutinfo, 'servicesinfo': servicesinfo, 'programinfo': programinfo})
英文:
i am trying to create a nav dropdown that with the model titles but it is showing drop down on the main html page but it is not showing other pages and also i tried to make a one header html template in it also its is not showing the titles , so how can i pass the objects to multiple template
my template drop down:
<li class="menu-has-children"><a href="#services">All Services</a>
<ul style="display: inline;">
{% for detailinfo in detail.all %}
<li><a href="{% url 'details' services.slug %}">{{ detailinfo.title }}</a></li>
{% endfor %}
</ul>
</li>
my view:
def details(request, services_slug):
q = services.objects.filter(slug=services_slug)
if q.exists():
q=q.first()
else:
return HttpResponse("<h1> page not found </h1>")
detail = {'detail': q}
return render(request, 'detail.html', detail,)
i want to add these objects in detail page
def homepage(request):
aboutinfo = aboutpage.objects
servicesinfo = services.objects
programinfo = prgm.objects
return render(request, 'index.html', {'aboutinfo': aboutinfo, 'servicesinfo': servicesinfo, 'programinfo': programinfo})
答案1
得分: 2
你不能。
如果你想在其他页面显示“detail”,那么页面视图必须具有“detail”。
我的方法是收集我在所有视图中使用的变量,并将它们与所有视图组合在一起。
例如;
# general.py
payload = {'detail': x_variable, 'loremipsum': 2}
这是我的通用payload,我在项目的每个视图中导入这个payload。
然后在所有视图中;
# 例如,你的视图
def details(request, services_slug):
......
detail = {'detail': q}
detail.update(my_imported_payload)
return render(request, 'detail.html', detail,)
重要编辑:有一种更好的方法来做这件事。请搜索Django中的上下文处理器!
英文:
You can't.
If you want showing detail
for other pages, the pages view must have detail
.
My method is to collect the variables I use everywhere and combine them with all views.
For example;
# general.py
payload = {'detail': x_variable, 'loremipsum': 2}
this is my general payload and i import this payload every view in my project.
then in all views;
# your view for example
def details(request, services_slug):
.........
detail = {'detail': q}
detail.update(my_imported_payload)
return render(request, 'detail.html', detail,)
IMPORTANT EDIT : There was a better way to do this. Please search the context processors in django!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论