Mohammed Shamma
Mohammed Shamma

Reputation: 35

How do I use the django-cms Python api to create a page?

I'm trying to write a CMS content migration script that will create our pages for django-cms. However even with the default install of Django-cms, Django-treebeard keeps raising exceptions and it's not clear to me why.

For example, when this line is executed:

api.create_page(title="Hello world",
    language='en',
    parent=None)

My script always fails here (line 307-8) in mp_tree.py (v 4.1.0) from treebeard:

if newobj.pk:
    raise NodeAlreadySaved("Attempted to add a tree node that is "\
                "already in the database")

Does anyone have a very simple example on how to use the cms.api.create_page() method?

http://docs.django-cms.org/en/release-3.4.x/reference/api_references.html?highlight=create_page#cms.api.create_page

Pip freeze shows these versions:

aldryn-apphooks-config==0.2.7
aldryn-boilerplates==0.7.4
aldryn-common==1.0.4
aldryn-faq==1.2.2
aldryn-forms==2.1.3
aldryn-reversion==1.0.9
aldryn-search==0.3.0
aldryn-translation-tools==0.2.1
appdirs==1.4.3
boto==2.46.1
boto3==1.4.4
botocore==1.5.26
cmsplugin-filer==1.1.3
contextlib2==0.5.4
cssselect==1.0.0
cssutils==1.0.1
dj-database-url==0.4.1
Django==1.8.17
django-absolute==0.3
django-admin-sortable==2.0.21
django-admin-sortable2==0.6.6
django-appconf==1.0.2
django-appdata==0.1.6
django-cas-ng==3.5.6
django-classy-tags==0.8.0
django-cms==3.4.1
django-cors-headers==2.0.2
django-debug-toolbar==1.7
django-emailit==0.2.2
django-environ==0.4.1
django-extensions==1.7.7
django-filer==1.2.5
django-formtools==1.0
django-haystack==2.5.1
django-mptt==0.8.6
django-parler==1.6.5
django-polymorphic==0.8.1
django-reversion==1.10.2
django-sekizai==0.10.0
Django-Select2==4.3.2
django-simple-captcha==0.5.3
django-sizefield==0.9.1
django-sortedm2m==1.3.2
django-spurl==0.6.4
django-standard-form==1.1.1
-e git+https://github.com/jschneier/django-storages.git@043b91b10ebfebdf7e752d743ae630f0daa2c4f4#egg=django_storages
django-storages-redux==1.3.2
django-tablib==3.1.2
django-taggit==0.21.3
django-treebeard==4.1.0
djangocms-admin-style==1.2.6.2
djangocms-attributes-field==0.1.2
djangocms-column==1.7.0
djangocms-googlemap==1.0.1
djangocms-installer==0.9.3
djangocms-link==2.0.3
djangocms-rest-api==0.2.0
djangocms-snippet==1.9.2
djangocms-style==2.0.1
djangocms-text-ckeditor==3.3.1
djangocms-video==2.0.3
djangorestframework==3.4.7
docutils==0.13.1
easy-thumbnails==2.3
google-auth==0.8.0
google-auth-httplib2==0.0.2
google-cloud-core==0.23.1
google-cloud-storage==0.23.1
googleapis-common-protos==1.5.2
html5lib==0.9999999
httplib2==0.10.3
jmespath==0.9.0
lxml==3.7.0
mysqlclient==1.3.10
packaging==16.8
Pillow==3.4.2
premailer==3.0.1
protobuf==3.2.0
psycopg2==2.6.2
pyasn1==0.2.3
pyasn1-modules==0.0.8
pyparsing==2.2.0
python-cas==1.2.0
python-dateutil==2.6.0
python-slugify==1.2.0
pytz==2016.10
raven==6.0.0
requests==2.12.3
rsa==3.4.2
s3transfer==0.1.10
six==1.10.0
sqlparse==0.2.3
tablib==0.11.3
tzlocal==1.3
Unidecode==0.4.19
URLObject==2.4.2
YURL==0.13

Upvotes: 1

Views: 1741

Answers (2)

Mohammed Shamma
Mohammed Shamma

Reputation: 35

So thanks to @dentemm and @paulo I was able to get this running in a Python shell as follows:

>>> from cms import constants
>>> from cms.constants import TEMPLATE_INHERITANCE_MAGIC
>>> from cms import api
>>> api.create_page(title="Hello World4",language='en',template=TEMPLATE_INHERITANCE_MAGIC,parent=None)

<cms.models.pagemodel.Page object at 0x102b674e0>

It was failing because I was not including the 'template' argument and the necessary imports.

Upvotes: 1

dentemm
dentemm

Reputation: 6379

I am unsure if it's the cause for your problems, but there is an error in your api.create_page() code above: parent=None shouldn't have spaces.

Upvotes: 0

Related Questions