向django-cms添加自定义占位符

时间:2014-10-15 15:07:22

标签: python django django-templates placeholder django-cms

我尝试创建自定义占位符以在网站上添加管理员可编辑的部分。 Django-cms' documentation对于添加它应该做些什么非常通用。

占位符位于一个名为 contacts 的单独应用中,已添加到 settings.py 文件中

这里是我的代码到目前为止(跳过导入更简洁):

models.py

class ContactsPlaceholder(models.Model):
    phone = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    address = models.TextField(max_length=700)
    # your fields
    my_placeholder = PlaceholderField('contacts_contactsplaceholder')
    # your methods

    def __unicode__(self):
        return "Contacts Placeholder"

views.py

def contacts(request):
  # Load the the contacts from db
  contactsfields = ContactsField.objects.all()
  contacts_placeholder = ContactsPlaceholder.objects.all() 
  context = {"contactsfields" : contactsfields, "contacts_placeholder" : contacts_placeholder}
  return render(request, "../templates/contacts.html", context) 

def contacts_placeholder_detail(request, id): # is this really useful?
  object = get_object_or_404(ContactsPlaceholder, id=id)
  return render_to_response('../templates/contacts_placeholder_detail.html', {
      'object': object,
  }, context_instance=RequestContext(request))

contacts.html

{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
<div>
  <h1>Contacts</h1>

  {% placeholder "contacts_placeholder" or %}
   <p>Placeholder is empty</p>
  {% endplaceholder %}
</div>
{% endblock content %}

最后, admin.py

class ContactsPlaceholderAdmin(PlaceholderAdminMixin, admin.ModelAdmin):
    # Editable fields
    frontend_editable_fields = ("phone", "email", "address")
    # Form fields order and sections
    fieldsets = [
        (None,{"fields" : ["phone", "email", "address"]})
    ]

admin.site.register(ContactsPlaceholder, ContactsPlaceholderAdmin)

使用此配置,在运行

python manage.py syncdb
后,我确实得到了#34;占位符为空&#34;在html响应中,当然占位符不能使用标准前端编辑器进行编辑,这是期望的行为。

转到 127.0.0.1:8000/admin / 我可以看到&#34;联系人占位符&#34;表,但是当我点击它或尝试添加项目时,我会收到以下错误:

OperationalError at /us/admin/contacts/contactsplaceholder/
no such column: contacts_contactsplaceholder.phone

1 个答案:

答案 0 :(得分:2)

如果通过app挂钩呈现contacts()视图,则在模板中使用{% placeholder "contacts_placeholder" %}将无效。

在通过AppHook连接到CMS的应用中使用占位符时,您必须使用{% static_placeholder "MyPlaceholder" %}而不是通常的{% Placeholder "MyPlaceholder" %}

如果你有一个应用程序或插件,你已经在其中创建了一个PlaceholderField,那么在你的模板中呈现它,你需要以下标记;

{% render_placeholder instance.placeholderfieldname %}