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评论75阅读模式
英文:

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:

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

i want to add these objects in detail page

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

得分: 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 = {&#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;

# your view for example
def details(request, services_slug):

.........

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

detail.update(my_imported_payload)

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:

确定