Note that there are some explanatory texts on larger screens.

plurals
  1. POExtend flatpages for a specific application
    primarykey
    data
    text
    <p>I have a portfolio application that lists projects and their detail pages. Every project generally has the same information (gallery, about etc. ), but sometimes the user might want to add extra information for a particularly large project, maybe an extra page about the funding of that page for example. </p> <p>Would it be possible to create an overwritten flatpages model within my portfolio app that forces any flatpages created within that application to always begin with /portfolio/project-name/flat-page. I could then simply pass the links to those flatpages associated with the project to the template so any flatpage the user generates will automatically be linked to from the project page. </p> <p><strong>EDIT</strong></p> <p>I have it somewhat working now</p> <p>So I overwrite the FlatPage model in my portfolio app as described:</p> <pre><code>from django.contrib.flatpages.models import FlatPage from project import Project from django.db import models from django.contrib.sites.models import Site class ProjectFlatPage(FlatPage): prefix = models.CharField(max_length=100) project = models.ForeignKey(Project) </code></pre> <p>which allows me to associate this flatpage with a particular project, </p> <p>Then I overwrite the save method to write all the extra information when a user saves (needs to be tidied up):</p> <pre><code>def save(self, force_insert=False, force_update=False): self.url = u"%s%s/" % (self.project.get_absolute_url(),self.prefix) self.enable_comments = False self.registration_required = False self.template_name = 'project/project_flatpage.html' super(FlatPage, self).save(force_insert, force_update) </code></pre> <p>and I scaleback the admin to just allow the important stuff:</p> <pre><code>class ProjectFlatPageForm(forms.ModelForm): prefix = forms.RegexField(label=_("Prefix"), max_length=100, regex=r'^[a-z0-9-]+$'), class Meta: model = ProjectFlatPage class ProjectnFlatPageAdmin(admin.ModelAdmin): form = ProjectFlatPageForm </code></pre> <p>so now the user can add a flat page inside my app and associate it with a particular project.</p> <p>In the admin, they just enter a slug for the page and it automatically gets appended through the save() method like: /projects/project-name/flat-page-name/</p> <p>The remaining problem is on the template end. I can access the normal flatpage information through the given template tags {{ flatpage.title }} and {{ flatpage.content }} put I have no access to the extra fields of the inherited model (i.e. the project field)</p> <p>Is there anyway around this?</p> <p><strong>EDIT2</strong> </p> <p>Having thought about it, the easiest way is to write a template tag to find the projectFlatPage associated with the flatpage and get access to it from there. A bit like overwritting the default flatpages template tags</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload