Django 平面页面默认站点
我主要在一个网站上使用平面页面(来自网站框架)。如何为所有创建的平面页面标记现有站点默认值? 每次为创建的每个页面选择相同的站点都是浪费时间。有什么方法可以在模型或保存方法中覆盖它吗?
mostly I use flatpages for one site (from sites framework). How can I mark existing site default for all created flatpages?
It is waste of time every time to choose same site for every page created. Is there any way to override this in models or save method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
允许站点字段(models.py -> FlatPage.sites)中为空白并将其放入模型表单(admin.py -> FlatPageForm)中
编辑:
我想出了一个更好的解决方案。
这使得当前站点已经专注于ManyToMany站点领域。编辑 flatpages/admin.py。在文件开头添加:
from django.contrib.sites.models import Site
并将以下代码放入class FlatPageAdmin
中:sites字段中不需要留空在 FlatPage 模型中
allow for blank in sites field (models.py -> FlatPage.sites) and put this in your model Form (admin.py -> FlatPageForm)
edit:
I've come up with a better solution.
This makes current site already focused in ManyToMany sites field. Edit flatpages/admin.py. Add :
from django.contrib.sites.models import Site
in the beggining of file and put following code intoclass FlatPageAdmin
:There is no need to allow for blank in sites field in FlatPage model
这是我的代码,它有效:
Here is my code, it works:
这是解决原始问题的一段完全有效的代码(标记所有创建的平面页面的现有站点默认值):
此外,此解决方案:
proxy = True
无需运行 syncbd 即可为CustomFlatPage
模型创建额外的表。Here is a completely working piece of code solving the original problem (to mark existing site default for all created flatpages):
Also this solution:
proxy = True
is not necessary to run syncbd in order to create an extra table forCustomFlatPage
model.我采取了不同的方法,猴子修补 FlatpageForm Meta 以使用网站的自定义小部件。
在使用管理之前,您必须从某个地方调用 flatpages() 。我将上面的代码放在包 Monkey_patches 的 admin.py 模块中,然后在我的根 urls.py 中(第一次请求传入时导入),我放入:
This is under Django 1.4 with python 2.7.3, but也应该适用于旧版本。
哦,请注意,如果您确实想要一个空的站点关联,您可以有意取消选择它,因为这只是小部件初始化。后续编辑将传递一个空列表以呈现为值,该值不是 None。
比尔,KE1G
I took a different approach, monkey patching the FlatpageForm Meta to use a custom widget for sites.
You must call flatpages() from somewhere before the admin might be used. I put the code above in a module admin.py in a package monkey_patches, then in my root urls.py, which gets imported the first time a request comes in, I put:
This is under Django 1.4 with python 2.7.3, but should work for older versions too.
Oh, and note that if you really want an empty sites association, you can deselect it intentionally, since this is just widget initialization. Subsequent edits will pass an empty list to render as value, which isn't None.
Bill, KE1G