我有一个名为blogengine的Django应用程序,它正如名称所暗示的那样。当flatgengine调用它们时,我似乎无法从blogengine(帖子)中获取数据以显示在模板中。我是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 %}
吗?
答案 0 :(得分:2)
我理解的问题如下:
flatpages/default.html <= default flatpages template overwrite
|_ incudes somehow template-sidebar_b.html <= your template
template-sidebar_b.html由flatpages-template包含(“调用”),但由于flatpage不知道模板变量“posts”,因此不会呈现任何内容。
可能的解决方案是:
如您所说创建模板标签=&gt; {%load posts%} =&gt; probaby是最好的解决方案,但只有在您的模板标签中不需要任何过滤器或内容时才可以访问模板,因为这样您再次遇到同样的问题。
使用您自己的=&gt;替换默认的 FlatpageFallbackMiddleware 轻松this和/或查看 django.contrib.flatpages.middleware.FlatpageFallbackMiddleware 的代码。我认为这是最好的解决方案,因为您可以访问请求和响应对象。
将您的帖子数据添加到默认的上下文处理器,使用RequestContext(docs)=&gt;呈现flatpages。我不 推荐这个
问候