views for Django flatpages?

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!

Answers


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)

Need Your Help

How to transfer onclick with a jsp variable and direct to other page

javascript mysql jsp sql-update

Do anyone know how to transfer variable from button to other page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.