如何访问 Django 模板中不相关的模型?

发布于 2024-09-10 19:47:45 字数 534 浏览 1 评论 0原文

我想使用一个应用程序来创建一个易于使用管理界面进行编辑的菜单。像这样:

class Menu_item
        name = models.CharField()
        item_url = models.URLField()

我的模板看起来像这样:

{% 扩展 base.html %}

div ID="nav"

 {{ foo.navbar.? }}

/div

div ID="内容"

 {% 块内容 %}{% endblock %}

/div

我希望 div#nav 包含基于上述模型的 ul,但只是不知道如何实现这一点。看起来 object_list 通用视图会很棒,但是 URL 访问填充 div#content 的模型的视图。有人有什么建议吗?有没有一种方法可以在没有 URL 的情况下访问通用视图?

谢谢。

I want to use an app to create a menu that is easy to edit with the admin interface. Something like this:

class Menu_item
        name = models.CharField()
        item_url = models.URLField()

My template looks something like this:

{% extends base.html %}

div ID="nav"

   {{ foo.navbar.? }}

/div

div ID="Content"

   {% block content %}{% endblock %}

/div

I want div#nav to contain a ul based upon the above model but just can't figure out how to accomplish this. It seems like an object_list generic view would be great but, the URL accesses the view for the model that populates div#content. Does anyone have any suggestions? Is there a way to access a generic view without a URL?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-09-17 19:47:51

首先,在您看来,从 db 获取数据:

def index(request):
    navbar = Menu_item.objects.all()
    return render_to_response( 'you_template.html', 
        { 'navbar': navbar }, context_instance = RequestContext (request ) )

在模板中:

<div id="nav">
    <ul>
    {% for i in navbar %}
        <li><a href="{{ i.item_url }}">{{ i.name }}</a></li>
    {% endfor  %}
    </ul>
</div>

First, in you view, get data from db:

def index(request):
    navbar = Menu_item.objects.all()
    return render_to_response( 'you_template.html', 
        { 'navbar': navbar }, context_instance = RequestContext (request ) )

And in template:

<div id="nav">
    <ul>
    {% for i in navbar %}
        <li><a href="{{ i.item_url }}">{{ i.name }}</a></li>
    {% endfor  %}
    </ul>
</div>
同展鸳鸯锦 2024-09-17 19:47:48

我发现了一个解决方案。包含标签。

我所做的是创建一个包含标签,它相当于一个简单的自定义模板标签(django 为您提供了一个快捷方式!)。

这是我所做的:

  • 忽略views.py - 在这种情况下不会使用它
  • 在应用程序的目录中创建一个名为“templatetags”的子目录,其中包含 init.py 和 get_navbar.py (或任何您希望标记为的内容) ):

    我的网站/
       导航栏/
            模板标签/
                __ init__.py (没有 init 之前的空格)
                获取导航栏.py
    
  • 我将 navbar.models 更改为如下所示:

    从 django.db 导入模型
    
    
    类 Menu_choice(models.Model):
            名称 = models.CharField(max_length=30)
    
            def __unicode__(自身):
                    返回自己的名字
    
    类 Menu_item(models.Model):
            名称 = models.CharField(max_length=20)
            导航栏 = models.ForeignKey(Menu_choice)
            item_url = models.CharField(max_length=200)
            item_desc = models.CharField(max_length=30)
    
            def __unicode__(自身):
                    返回自己的名字
    

    这允许我在管理界面中为每一层导航(主要、次要等)创建一个 Menu_choice 对象

  • get_navbar.py 如下所示:

    from navbar.models import Menu_choice, Menu_item
    从 django 导入模板
    
    注册 = template.Library()
    
    @register.inclusion_tag('navbar/navbar.html')
    def get_navbar(navBar):
            导航栏 = Menu_item.objects.filter(navbar__name=navBar)
            返回 {“导航栏”:导航栏}
    

    (注意导航栏而不是导航栏)

  • 创建 navbar.html 模板:
    <前><代码>


  • 最后,我将以下内容插入到我的 base.html 中:
    {% load get_navbar %}

    我想要主导航的位置:

    {% get_navbar "primary" %}

    注意:发送到包含标签的字符串周围的引号很重要。在我弄清楚这一点之前,我花了相当长的时间试图让它发挥作用。

    同时非常感谢 dikamilo 的帮助。

  • I have discovered a solution. Inclusion Tags.

    What I did was create an inclusion tag, which amounts to a simple custom template tag (django provides a shortcut for you!).

    Here is what I did:

  • Ignore views.py - It will not be used in this case
  • Create a sub-directory in the app's dir called "templatetags" containing init.py and get_navbar.py (or whatever you want your tag to be):

    mysite/
       navbar/
            templatetags/
                __ init__.py (without the space befire init)
                get_navbar.py
    
  • I changed my navbar.models to look like this:

    from django.db import models
    
    
    class Menu_choice(models.Model):
            name = models.CharField(max_length=30)
    
            def __unicode__(self):
                    return self.name
    
    class Menu_item(models.Model):
            name = models.CharField(max_length=20)
            navbar = models.ForeignKey(Menu_choice)
            item_url = models.CharField(max_length=200)
            item_desc = models.CharField(max_length=30)
    
            def __unicode__(self):
                    return self.name
    

    This allows me to create a Menu_choice object in the admin interface for each layer of navigation (primary, secondary, etc)

  • get_navbar.py looks like this:

    from navbar.models import Menu_choice, Menu_item
    from django import template
    
    register = template.Library()
    
    @register.inclusion_tag('navbar/navbar.html')
    def get_navbar(navBar):
            navbar = Menu_item.objects.filter(navbar__name=navBar)
            return { "navbar": navbar }
    

    (Note navBar as opposed to navbar)

  • Create the navbar.html template:
        <ul>
    {% for menu_item in navbar %} <li><a href="{{ menu_item.item_url }}">{{ menu_item.name }}</a></li> {% endfor %} </ul>
  • Finally, I inserted the following into my base.html:
    {% load get_navbar %}

    And where I want primary navigation:

    {% get_navbar "primary" %}

    Note: the quotes around the string you are sending to your inclusion tag are important. I spent a ridiculously lengthy bit of time trying to make it work before I figured that out.

    Also a big thank you to dikamilo for the help.

  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文