Reputation: 4714
I use the following to determine the template on a pattern that matches the index page, in which I determine which template to load based on if it's an AJAX request:
def home(request):
if request.is_ajax():
template = "ajax.html"
else:
template = "index.html"
entries = posts.objects.all()[:10]
return render_to_response(template, {'posts' : entries}, context_instance=RequestContext(request))
However, I don't have anything regarding flatpages mentioned in my views.py, how can I do a similar request.is_ajax() check for the templates of flatpages?
Thanks!
Upvotes: 3
Views: 946
Reputation: 4714
Got it!
urls.py
url(r'^(?P<url>.*)$', 'Alpha.blog.views.flatpage', {}, 'flatpage'),
views.py:
def flatpage(request, url):
try:
if url == '':
url = 'home/'
flatpage = FlatPage.objects.get(url="/%s" % url)
except:
pass
if request.is_ajax():
template = 'pages/ajax.html'
else:
template = 'pages/default.html'
context_instance=RequestContext(request)
context_instance.autoescape=False
return render_to_response(template, {'flatpage': flatpage}, context_instance)
Upvotes: 5