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

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

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:

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

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

My main page view:

def homepage(request):

    aboutinfo = aboutpage.objects
    servicesinfo = services.objects
    programinfo = prgm.objects

    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

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

my view

def details(request, services_slug):
q = services.objects.filter(slug=services_slug)
if q.exists():
    q=q.first()
else:
    return HttpResponse(&quot;&lt;h1&gt; page not found &lt;/h1&gt;&quot;)

detail = {&#39;detail&#39;: q}

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

def homepage(request):

aboutinfo = aboutpage.objects
servicesinfo = services.objects
programinfo = prgm.objects

return render(request, &#39;index.html&#39;, {&#39;aboutinfo&#39;: aboutinfo, &#39;servicesinfo&#39;: servicesinfo, 
&#39;programinfo&#39;: programinfo})

答案1

得分: 1

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

servicesinfo = services.objects.all()

英文:

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

 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:

确定