对象标题未传递到多个页面的下拉菜单中。

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

objects titles are not passing to dropdown in multiple pages

问题

I created a navbar for each page and I want to include a dropdown by passing the model object title to the list. It's passing to the main page but not passing to other pages.

My template:

  1. <li class="menu-has-children"><a href="#services">All 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>

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("<h1>Page not found</h1>")
  7. detail = {'detail': q}
  8. return render(request, 'detail.html', detail)

And this is my main page view. It passes the titles to it but not to the detail page.

My main page view:

  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})

(Note: I've removed the HTML escaping for better readability, but you should make sure to include it in your actual code if needed.)

英文:

i created nav bar for ea ch pages and i want include a dropdown by passing model object title to the list
and its passing to main page but not passing to other pages

my template

  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,)

and my this is my main page view it passed the titles in it but not in the detail

my main page view

  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,
  6. &#39;programinfo&#39;: programinfo})

答案1

得分: 1

你需要在对象后面添加 .all() 来检索所有对象。

servicesinfo = services.objects.all()

英文:

you have to add .all() after the object to retrieve all objects

  1. servicesinfo= services.objects.all()

huangapple
  • 本文由 发表于 2020年1月6日 20:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611854.html
匿名

发表评论

匿名网友

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

确定