Reputation: 5513
I have a Django app called blogengine which does exactly what the name implies. I can't seem to get data from blogengine (posts) to display in templates when they're called by flatpages. I'm new to Django but I assume this is a urls.py issue.
My 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))
Would it be better practice to create a tag so I could just call {% load posts %}
when and where I need it?
Upvotes: 0
Views: 460
Reputation: 808
i understood the problem as follows:
flatpages/default.html <= default flatpages template overwrite
|_ incudes somehow template-sidebar_b.html <= your template
template-sidebar_b.html is included ("called") by the flatpages-template, but because the flatpage doesnt know a template variable "posts" nothing is rendered.
possible solutions are:
create a templatetag as you said => {% load posts %} => probaby the best solution but only possible if you dont need any filters or stuff in your templatetag you dont have access to in your template because then you have the same problem again.
replace the default FlatpageFallbackMiddleware with your own => easy as this and/or look into the code of django.contrib.flatpages.middleware.FlatpageFallbackMiddleware. i think this is the best solution because you have access to the request and response object.
add your posts data to a default contextprocessor, the flatpages are rendered with RequestContext (docs) => i do not recommend this
regards
Upvotes: 2