Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to field in extended flatpage in django

How to access field in extended flatpage in django?
I wrote this:

class ExtendedFlatPage(FlatPage):
     teaser = CharField(max_length=150)

class ExtendedFlatPageForm(FlatpageForm):
     teaser = CharField(max_length=150)
     class Meta:
        model = ExtendedFlatPage

class ExtendedFlatPageAdmin(FlatPageAdmin):
    form = ExtendedFlatPageForm
    fieldsets = (
        (None, {'fields': ('url', 'title', 'teaser', 'content', 'sites',)}),
    )     

admin.site.unregister(FlatPage)
admin.site.register(ExtendedFlatPage, ExtendedFlatPageAdmin)

And creation in admin is ok. But then in flatpages/default.html I tried this:

<html>
<body>
<h1>{{ flatpage.title }}</h1>
<strong>{{ flatpage.teaser }}</strong>
<p>{{ flatpage.content }}</p>
</body>
</html>

And there was no flatpage.teaser! What is wrong?

like image 662
Stan Avatar asked Dec 22 '10 09:12

Stan


1 Answers

Yes, as rebus mentioned the FlatpageFallbackMiddleware will pass default FlatPage model instance to the template. But in your case template variable {{ flatpage }} will also remember if it's ExtendedFlatPage instance, as described here in django-docs.

So to treat your flatpage as ExtendedFlatPage you have to use:

{{ flatpage.extendedflatpage.teaser }} instead of {{ flatpage.teaser }}.

like image 189
Dzejkob Avatar answered Sep 22 '22 13:09

Dzejkob