开发者

Extend flatpages for a specific application

开发者 https://www.devze.com 2023-01-21 14:24 出处:网络
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 infor

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.

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 generate开发者_Python百科s will automatically be linked to from the project page.

EDIT

I have it somewhat working now

So I overwrite the FlatPage model in my portfolio app as described:

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)

which allows me to associate this flatpage with a particular project,

Then I overwrite the save method to write all the extra information when a user saves (needs to be tidied up):

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)

and I scaleback the admin to just allow the important stuff:

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

so now the user can add a flat page inside my app and associate it with a particular project.

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/

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)

Is there anyway around this?

EDIT2

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


re the 'Edit 2' part of your question... since your custom flatpage model inherits from FlatPage there is an automatic one-to-one field relation created between them by Django.

So in your template, if you know that the flatpage object passed in is one of your customised ones you can access your extra attributes by:

{{ flatpage.projectflatpage.project }}


Of course. Just write your own flatpage-model, for example:

from django.contrib.flatpages.models import FlatPage

class MyFlatPage(FlatPage):
    prefix = models.CharField(max_length=100, editable=False)
    url = models.CharField(_('URL'), max_length=100, db_index=True, editable=False)

Then add a proper MyFlatPageAdmin to your admin.py file (if you want to, you can import the flatpageadmin from django.contrib.flatpages.admin and inherit from it). After all, you're using the flatpage-model, but overwrite the urls-field. Now add a signal, which concats the prefix and a automatically generated url suffix to the url (like the object id). You can now add the custom flatpage like:

flatpage = MyFlatPage(prefix='/portfolio/my-super-project/')

Everything clear now?

edit 1

As you can read in the documentation, every flatpage should be a projectflatpage and vice versa.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号