0

我有一个名为 blogengine 的 Django 应用程序,它的功能正如其名称所暗示的那样。当平面页面调用模板时,我似乎无法从博客引擎(帖子)中获取数据以显示在模板中。我是 Django 新手,但我认为这是 urls.py 问题。

我的 urls.py:

from django.conf.urls.defaults import patterns, include, url
from blogengine.views import PostsFeed

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    # Home page
    url(r'^$', 'blogengine.views.getPosts'),
    url(r'^(\d+)/?$', 'blogengine.views.getPosts'),

    # tinyMCE
    (r'^tinymce/', include('tinymce.urls')),

    # Blog posts
    url(r'^\d{4}/\d{1,2}/([-a-zA-Z0-9]+)/?$', 'blogengine.views.getPost'),

    # Categories
    url(r'^categories/(\w+)/?$', 'blogengine.views.getCategory'),
    url(r'^categories/(\w+)/(\d+)/?$', 'blogengine.views.getCategory'),

    # Comments
    #url(r'^comments/', include('django.contrib.comments.urls')),

    # RSS feeds
    url(r'^feeds/posts/$', PostsFeed()),


    # Flat pages
    #url(r'', include('django.contrib.flatpages.urls')),
    #not needed since '...middleware.FlatpageFallbackMiddleware' is installed in settings.py

)

模板-sidebar_b.html:

<div class="grid_4 omega">
{% if posts %}
    {% for post in posts %}
            {% if post.featured %}
            <h3>Featured</h3>       

            <div class="featured-post">

                <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
                <p class="post-info">Posted by <a href="index.html">{{ post.author.first_name }} {{ post.author.last_name }}</a> on {{ post.pub_date|date:"m.d.y"  }} </p>
                <p>
                <a href="http://getfirefox.com/"><img src="static/images/image.gif" width="160" height="100" alt="firefox" class="float-left" /></a>

                {{ post.text|safe|truncatechars:200 }}

                </p>    

                <p><a class="more-link" href="{{ post.get_absolute_url }}">continue reading</a></p>

            </div>
            {% endif %}
    {% endfor %}
    {% else %}
    <h3>Featured</h3>
    <div class="featured-post">
        <h4>Nothing to show...</h4>
    </div>
{% endif %}
</div>  

视图.py:

from django.shortcuts import render_to_response
from django.core.paginator import Paginator, EmptyPage
from blogengine.models import Post, Category
from django.template import RequestContext
from django.contrib.syndication.views import Feed

from django.template.loader import add_to_builtins
add_to_builtins('blogengine.templatetags.tags')

def getPosts(request, selected_page=1):
    # Get all blog posts
    posts = Post.objects.all().order_by('-pub_date')
    # Add pagination
    pages = Paginator(posts, 5)
    # Get the specified page
    try:
        returned_page = pages.page(selected_page)
    except EmptyPage:
        returned_page = pages.page(pages.num_pages)

    # Display all the posts
    return render_to_response('posts.html', { 'posts':returned_page.object_list, 'page':returned_page}, RequestContext(request))


def getPost(request, postSlug):
    # Get specified post
    post = Post.objects.filter(slug=postSlug)
    # Display specified post
    return render_to_response('single.html', { 'posts':post}, RequestContext(request))

def getCategory(request, categorySlug, selected_page=1):
    # Get specified category
    posts = Post.objects.all().order_by('-pub_date')
    category_posts = []
    for post in posts:
        if post.categories.filter(slug=categorySlug):
            category_posts.append(post)
    # Add pagination
    pages = Paginator(category_posts, 5)
    # Get the category
    category = Category.objects.filter(slug=categorySlug)[0]
    # Get the specified page
    try:
        returned_page = pages.page(selected_page)
    except EmptyPage:
        returned_page = pages.page(pages.num_pages)
    # Display all the posts
    return render_to_response('category.html', { 'posts': returned_page.object_list, 'page': returned_page, 'category': category}, RequestContext(request))

创建一个标签会更好吗?这样我就可以在{% load posts %}需要的时间和地点调用它?

4

1 回答 1

2

我对问题的理解如下:

flatpages/default.html <= default flatpages template overwrite
   |_ incudes somehow template-sidebar_b.html <= your template

template-sidebar_b.html 被 flatpages-template 包含(“调用”),但是因为 flatpage 不知道模板变量“posts”,所以没有呈现任何内容。

可能的解决方案是:

  • 如你所说创建一个模板标签=> {% load posts %} => 可能是最好的解决方案,但只有在你的模板标签中不需要任何过滤器或东西时你才能在模板中访问,因为那样你又遇到了同样的问题.

  • 用您自己的 => easy as this替换默认的FlatpageFallbackMiddleware和/或查看django.contrib.flatpages.middleware.FlatpageFallbackMiddleware的代码。我认为这是最好的解决方案,因为您可以访问请求和响应对象。

  • 将您的帖子数据添加到默认上下文处理器,平面页面使用 RequestContext ( docs ) => 我不推荐这样做

问候

于 2012-12-20T16:20:44.107 回答