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

)。

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

views.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 %} => おそらく最良の解決策ですが、テンプレートタグにフィルターやものが必要ない場合にのみ可能です。テンプレートでアクセスできないため、同じ問題が再び発生します.

  • デフォルトのFlatpageFallbackMiddlewareを独自の => 簡単なものに置き換えるか、 django.contrib.flatpages.middleware.FlatpageFallbackMiddlewareのコードを調べてください。リクエストとレスポンスのオブジェクトにアクセスできるので、これが最善の解決策だと思います。

  • 投稿データをデフォルトの contextprocessor に追加します。フラットページは RequestContext ( docs ) でレンダリングされます => これはお勧めしません

よろしく

于 2012-12-20T16:20:44.107 に答える