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

huangapple go评论113阅读模式
英文:

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模板,但标题也不显示,那么我如何将对象传递给多个模板?

我的模板下拉菜单

  1. <li class="menu-has-children"><a href="#services">所有服务</a>
  2. <ul style="display: inline;">
  3. {% for detailinfo in detail.all %}
  4. <li><a href="{% url 'details' services.slug %}">{{ detailinfo.title }}</a></li>
  5. {% endfor %}
  6. </ul>
  7. </li>

我的视图

  1. def details(request, services_slug):
  2. q = services.objects.filter(slug=services_slug)
  3. if q.exists():
  4. q = q.first()
  5. else:
  6. return HttpResponse("<h1>页面未找到</h1>")
  7. detail = {'detail': q}
  8. return render(request, 'detail.html', detail,)

我想在详情页面中添加这些对象。

  1. def homepage(request):
  2. aboutinfo = aboutpage.objects
  3. servicesinfo = services.objects
  4. programinfo = prgm.objects
  5. 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:

  1. &lt;li class=&quot;menu-has-children&quot;&gt;&lt;a href=&quot;#services&quot;&gt;All Services&lt;/a&gt;
  2. &lt;ul style=&quot;display: inline;&quot;&gt;
  3. {% for detailinfo in detail.all %}
  4. &lt;li&gt;&lt;a href=&quot;{% url &#39;details&#39; services.slug %}&quot;&gt;{{ detailinfo.title }}&lt;/a&gt;&lt;/li&gt;
  5. {% endfor %}
  6. &lt;/ul&gt;
  7. &lt;/li&gt;

my view:

  1. def details(request, services_slug):
  2. q = services.objects.filter(slug=services_slug)
  3. if q.exists():
  4. q=q.first()
  5. else:
  6. return HttpResponse(&quot;&lt;h1&gt; page not found &lt;/h1&gt;&quot;)
  7. detail = {&#39;detail&#39;: q}
  8. return render(request, &#39;detail.html&#39;, detail,)

i want to add these objects in detail page

  1. def homepage(request):
  2. aboutinfo = aboutpage.objects
  3. servicesinfo = services.objects
  4. programinfo = prgm.objects
  5. return render(request, &#39;index.html&#39;, {&#39;aboutinfo&#39;: aboutinfo, &#39;servicesinfo&#39;: servicesinfo, &#39;programinfo&#39;: programinfo})

答案1

得分: 2

你不能。
如果你想在其他页面显示“detail”,那么页面视图必须具有“detail”。
我的方法是收集我在所有视图中使用的变量,并将它们与所有视图组合在一起。
例如;

  1. # general.py
  2. payload = {'detail': x_variable, 'loremipsum': 2}

这是我的通用payload,我在项目的每个视图中导入这个payload。
然后在所有视图中;

  1. # 例如,你的视图
  2. def details(request, services_slug):
  3. ......
  4. detail = {'detail': q}
  5. detail.update(my_imported_payload)
  6. 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;

  1. # general.py
  2. payload = {&#39;detail&#39;: x_variable, &#39;loremipsum&#39;: 2}

this is my general payload and i import this payload every view in my project.
then in all views;

  1. # your view for example
  2. def details(request, services_slug):
  3. .........
  4. detail = {&#39;detail&#39;: q}
  5. detail.update(my_imported_payload)
  6. return render(request, &#39;detail.html&#39;, detail,)

IMPORTANT EDIT : There was a better way to do this. Please search the context processors in django!

huangapple
  • 本文由 发表于 2020年1月3日 14:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定