为特定应用程序扩展平面页面
我有一个投资组合应用程序,其中列出了项目及其详细信息页面。每个项目通常都有相同的信息(图库、关于等),但有时用户可能想要为特别大的项目添加额外的信息,例如可能是关于该页面的资金的额外页面。
是否可以在我的作品集应用程序中创建一个覆盖的平面页面模型,强制在该应用程序中创建的任何平面页面始终以 /portfolio/project-name/flat-page 开头。然后,我可以简单地将指向与项目关联的平面页面的链接传递到模板,以便用户生成的任何平面页面将自动从项目页面链接到。
编辑
我现在已经有点工作
所以我按照描述覆盖我的投资组合应用程序中的FlatPage模型:
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)
这允许我将此平面页面与特定项目相关联,
然后我覆盖保存方法以写入所有额外的内容用户保存时的信息(需要整理):
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)
我缩小了管理范围,只允许重要的内容:
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
所以现在用户可以在我的应用程序中添加一个平面页面并将其与特定项目相关联。
在管理中,他们只需为页面输入一个 slug,它就会通过 save() 方法自动附加,例如: /projects/project-name/flat-page-name/
剩下的问题在于模板端。 但我无法访问继承模型的额外字段(即项目字段)
我可以通过给定的模板标签 {{ flatpage.title }} 和 {{ flatpage.content }} 访问正常的平面信息, 这?
EDIT2
经过考虑,最简单的方法是编写一个模板标签来查找与平面页面关联的projectFlatPage并从那里访问它。有点像覆盖默认的 Flatpages 模板标签
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 generates 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-]+
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
),
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于您问题的“编辑 2”部分...由于您的自定义 Flatpage 模型继承自 FlatPage,因此 Django 在它们之间创建了自动的一对一字段关系。
因此,在您的模板中,如果您知道传入的平面对象是您的自定义对象之一,您可以通过以下方式访问额外的属性:
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-model,例如:
然后将适当的 MyFlatPageAdmin 添加到您的 admin.py 文件中(如果您愿意,您可以从 django.contrib.flatpages.admin 导入 flatpageadmin 并从中继承)。毕竟,您使用的是 flatpage-model,但覆盖了 urls-field。现在添加一个信号,它将前缀和自动生成的 url 后缀连接到 url(如对象 id)。您现在可以添加自定义平面,例如:
现在一切都清楚了吗?
编辑1
正如您可以阅读在文档中,每个平面页面都应该是项目平面页面,反之亦然。
Of course. Just write your own flatpage-model, for example:
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:
Everything clear now?
edit 1
As you can read in the documentation, every flatpage should be a projectflatpage and vice versa.