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?
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 }}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With