views:

181

answers:

2

I use django flatpages for a lot of content on our site, I'd like to extend it to accept django template tags in the content as well.

I found this snippet but after much larking about I couldn't get it to work. Am I correct in assuming that you would need too "subclass" the django flatpages app to get this to work? Is this best way of doing it? I'm not quite sure how to structure it, as I don't really want to directly modify the django distribution.

A: 

An alternative approach could be to write a simple app based on the direct_to_template generic view:

Carles Barrobés
Hi Carles, This wouldn't help me as I have a lot of text entered and maintained through the flat pages admin interface, the issue is I need to be able to enter django template tags in there as well as the plain text, and have them render.
Tristan
+2  A: 

1. A simple page view wich will render template tags by loading a template for each page:

in url.py

url(r'^page/(?P<slug>.*)/$','my_app.views.page_detail', name='page_url'),

in my_app/views.py

def page_detail (request, slug):
    return render_to_response('page/' + slug + '.html', {},
                              context_instance=RequestContext(request))

2. Another method with flat pages stored in database, is to use a "template evaluation tag" in your template like this one.

edit You just have to modify flatpages template like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
<html>
<head>
<title>{{ flatpage.title }}</title>
</head>
<body>
{% load evaluate_tag %} 
{% evaluate flatpage.content %} 
</body>
</html>
Pierre-Jean Coudert
Hi Pierre,The problem is I have a lot of content in the flat pages app, but I often want to be able to include Django Tags as well and enter them through the flat pages admin interface, I don't want to keep the content separately in templates as I think your example shows? I did take a look at the template evaluation tag you suggested, but I'm having trouble understanding how that would help me achieve me goal
Tristan
Tristan, I've edited my response and added a detailed template. Hope it helps.
Pierre-Jean Coudert
Hi Pierre, with your example I better understand how the evaluate tag works, and that looks like its the solution for me, thank you for taking the time to explain. As I understand it, the evaluate_tag is just going to read through the output of the flatpage and process the relevant tags at that point in time. Looks perfect...
Tristan