在django-cms中添加自定义占位符

lymnna71  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(129)

我试图创建一个自定义占位符添加一个网站上的管理员可编辑部分。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))

联系人.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

我确实在html响应中得到了“Placeholder is empty”,当然,占位符不能使用标准的前端编辑器进行编辑,这是期望的行为。
转到 127.0.0.1:8000/admin/ 我可以看到“Contacts Placeholder”表,但是当我单击它或尝试添加项目时,我确实得到以下错误:

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

wtzytmuj

wtzytmuj1#

如果contacts()视图是通过应用钩子呈现的,那么在模板中使用{% placeholder "contacts_placeholder" %}将不起作用。
在通过AppHook连接到CMS的应用程序中使用占位符时,必须使用{% static_placeholder "MyPlaceholder" %}而不是通常的{% placeholder "MyPlaceholder" %}
如果你有一个应用程序或插件,你已经创建了一个PlaceholderField,然后在你的模板呈现,你需要以下标签;

{% render_placeholder instance.placeholderfieldname %}

字符串

相关问题