Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django从主模板中的外部应用程序获取上下文_Python_Django - Fatal编程技术网

Python django从主模板中的外部应用程序获取上下文

Python django从主模板中的外部应用程序获取上下文,python,django,Python,Django,我已经融入了我的项目。在主页侧边栏中,我想显示最新的博客文章。问题是,我的主页是django cms页面,我无法获取博客文章对象。URL.py: urlpatterns = i18n_patterns('', url(r'^admin/', include(admin.site.urls)), ..... url(r'djangocms_blog/', include('djangocms_blog.urls', namespace='djangocms_blog')),

我已经融入了我的项目。在主页侧边栏中,我想显示最新的博客文章。问题是,我的主页是django cms页面,我无法获取博客文章对象。URL.py:

urlpatterns = i18n_patterns('',
    url(r'^admin/', include(admin.site.urls)),
    .....
    url(r'djangocms_blog/', include('djangocms_blog.urls', namespace='djangocms_blog')),
    url(r'^', include('cms.urls')),

)
不知何故,在main.html中,我应该能够获得博客记录:

    {% extends "base.html" %} 
<nav class="secondary-menu">
                <ul class="nav">
                    <!-- i need display them here -->
                </ul>
            </nav>
{%extends“base.html”%}

有什么优雅的方法可以做到这一点吗?

使用
分配标签
包含标签

模板标签 包含标记的代码段

{posts%%中的post为%s}
{{post}}

{%endfor%}
你的家/任何模板

{%load blog_tags%}
{%latest_posts_as limit=20 as posts%}
{posts%%中的post为%s}
{{post}}

{%endfor%} {%latest\u posts\u inline limit=10%}

限制是可选的,但可能很方便:)

提出了其他解决方案。创建了新的django cms插件:

class BlogLatestEntriesPluginNav(BlogLatestEntriesPlugin):
    name = _('Navigition latest blog entries')
    render_template = 'latest_entries_nav.html'

plugin_pool.register_plugin(BlogLatestEntriesPluginNav)
其中
BlogLatestEntriesPlugin
是应用程序的插件。并创建了新模板
'latest\u entries\u nav.html'
。然后在
main.html

<ul class="nav">
    {% placeholder "left_sidebar" %}
</ul>
    {%占位符“左侧边栏”%}

创建了占位符并添加了该插件。

您可以使用-或者只使用提供的插件之一和cms。尝试使用templatetags,但仍然不起作用。我必须能够循环通过模板中的帖子。如果我创建模板标记,比如说
get\u latest\u posts()
并返回对象列表,我就不能循环它们。在模板中:
{%get\u latest\u posts as posts%}
引发错误
'get\u latest\u posts'接收的位置参数太多
<!-- your_template.html -->
{% load  blog_tags %}
<div>

    <!-- using assignment_tag -->
    {% latest_posts_as limit=20 as posts %}
    {% for post in posts %}
        <p>{{ post }}</p>
    {% endfor %}

    <!-- using inclusion_tag -->
    {% latest_posts_inline limit=10 %}

</div>
class BlogLatestEntriesPluginNav(BlogLatestEntriesPlugin):
    name = _('Navigition latest blog entries')
    render_template = 'latest_entries_nav.html'

plugin_pool.register_plugin(BlogLatestEntriesPluginNav)
<ul class="nav">
    {% placeholder "left_sidebar" %}
</ul>