====== Django-cms Installation ======
* http://django-cms.readthedocs.org/en/latest/how_to/install.html#installing
User virtualenv and pip to avoid dependency problems.
$ yum install python2.6 python-setuptools python-imaging
virtualenv --no-site-packages mysite-env
"No site packages" create isolated env from host env.
Install Django. From version 1.7.0 django has built-in migration, south is not needed.
source mysite-env/bin/activate
(env)$ pip install django django-cms south
(env)$ django-admin.py startproject mycmsproject
===== Installing Packages =====
==== Image Support ====
Install JPEG support on host (outside virtualenv!):
sudo yum install python-devel libjpeg-devel freetype-devel libpng-devel
sudo yum install mysql-devel mysql-common mysql-libs
Reinstall Pillow
pip install -I Pillow
Mysql-python
pip install mysql-python
==== Packages ====
Following django packages are listed in requirements.txt:
# Bare minimum
django-cms>=3.0
# These dependencies are brought in by django CMS, but if you want to
# lock-in their version, specify them
Django>=1.7
South==1.0.2 # Only needed for Django < 1.7
django-mptt==0.6.1
django-sekizai==0.7
django-classy-tags==0.5
djangocms-admin-style==0.2.2
six==1.3.0
# Optional, recommended packages
Pillow>=2
django-filer==0.9.8
cmsplugin-filer==0.10.1
django-reversion==1.8
# Server packages
mysql
django-debug-toolbar
Install those packages:
pip install -r requirements.txt
Install third-party app in current project:
(vfoss_env)[td@localhost vfoss_org]$ pip install --install-option="--install-purelib=$(pwd)" easy_thumbnails
Dump libs:
$ pip freeze>requirements.txt
$ pip install -r requirements.txt
==== Create a new Django project ====
django-admin.py startproject myproject
==== Configuring your project for django CMS ====
Open the settings.py file in your project.
To make your life easier, add the following at the top of the file:
# -*- coding: utf-8 -*-
import os
gettext = lambda s: s
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Add the following apps to your INSTALLED_APPS. This includes django CMS itself as well as its dependencies and other highly recommended applications/libraries:
'cms', # django CMS itself
'treebeard', # utilities for implementing a tree
'menus', # helper for model independent hierarchical website navigation
'south', # Only needed for Django < 1.7
'sekizai', # for javascript and css management
'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list **before** 'django.contrib.admin'.
'django.contrib.messages', # to enable messages framework (see :ref:`Enable messages `)
==== Install Module with Pip ====
pip install git+https://github.com/nadobit/django-filer.git@support/0.9.8
===== Debug-Toolbar =====
Bug: https://github.com/django-debug-toolbar/django-debug-toolbar/issues/457
Setup Debug-toolbar explicitly as followed:
http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup
====== Adding django-cms Plugins ======
===== Create New Django Project =====
django-admin startproject vfoss_org
===== Configure Django-cms app (django 1.8) =====
* http://django-cms.readthedocs.org/en/latest/how_to/install.html#installing
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# -*- coding: utf-8 -*-
import os
gettext = lambda s: s
#BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Application definition
INSTALLED_APPS = (
'django.contrib.sites'
'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list **before** 'django.contrib.admin'.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages', # to enable messages framework (see :ref:`Enable messages `)
'django.contrib.staticfiles',
'reversion',
'cms', # django CMS itself
'treebeard', # utilities for implementing a tree
'menus', # helper for model independent hierarchical website navigation
#'south', # Only needed for Django < 1.7
'sekizai', # for javascript and css management
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.common.CommonMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
)
TEMPLATE_DIRS = (
# The docs say it should be absolute path: BASE_DIR is precisely one.
# Life is wonderful!
os.path.join(BASE_DIR, "templates"),
)
CMS_TEMPLATES = (
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
ROOT_URLCONF = 'vfoss_org.urls'
WSGI_APPLICATION = 'vfoss_org.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Site
SITE_ID = 1
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
#For uploaded files, you will need to set up the MEDIA_ROOT setting:
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
===== Migration with Django 1.7, 1.8 =====
During transition from South to Django 1.7, we have to specify which app use which migration solution using
137 MIGRATION_MODULES = {
138 ## not needed for django 1.7.8 from v.3.0. Defaul migration folder is cms.migrations.
139 #'cms': 'cms.migrations_django',
140 #'menus': 'menus.migrations_django',
141
142 ## Still using migrations_django for django 1.7 migrations.
143 'filer': 'filer.migrations_django',
144 'cmsplugin_filer_image': 'cmsplugin_filer_image.migrations_django',
145 'djangocms_text_ckeditor': 'djangocms_text_ckeditor.migrations_django',
146
147 ## not checked. vim 0001* in app/migrations to see if it use django migration or South.
148 # Add also the following modules if you're using these plugins:
149 #'djangocms_file': 'djangocms_file.migrations_django',
150 #'djangocms_flash': 'djangocms_flash.migrations_django',
151 #'djangocms_googlemap': 'djangocms_googlemap.migrations_django',
152 #'djangocms_inherit': 'djangocms_inherit.migrations_django',
153 #'djangocms_link': 'djangocms_link.migrations_django',
154 #'djangocms_picture': 'djangocms_picture.migrations_django',
155 #'djangocms_snippet': 'djangocms_snippet.migrations_django',
156 #'djangocms_teaser': 'djangocms_teaser.migrations_django',
157 #'djangocms_video': 'djangocms_video.migrations_django',
158 #'aldryn_news': 'aldryn_news.migrations_django',
159 }
====== First Run ======
From django 1.7 no syncdb but using South:
./manage.py makemigrations
./manage.py migrate
python manage.py migrate app_name
./manage.py cms check
#admin
python manage.py createsuperuser
python manage.py validate
python manage.py runserver
Django migration tracks app migration using database:
+-----------------------+
| Tables_in_vfossdb |
+-----------------------+
| django_migrations |
| django_select2_keymap |
+-----------------------+
"makemigrations" will create migration files and "migrate" actually execute those files. In case database table of an app already exists, using "--fake" will skip creating the table.
===== CMSPlugin App Hook =====
**restart django-cms after adding apphook to a page**
====== Troubleshooting ======
===== Migration Issues =====
==== ERROR: migration invalid base ====
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for []
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more
==== Remove (squash all migration) ====
./manage.py migrate --fake cms zero
or try remove old migrations files and recreate them:
rm cmsplugin_vfoss_project/migrations/0001*
./manage.py makemigrations
./manage.py migrate
==== Error: PEP440Warning ====
error: Could not find suitable distribution for Requirement.parse('django-mptt==0.5.2,==0.6,==0.6.1')
** Diagnose Version Problem **
=== Version is specified in setup.py of the package. ===
old:
'django-mptt==0.5.2,==0.6,==0.6.1',
new, someone says never use '==':
'django-mptt>=0.6,<0.6.2',
=== Problem ===
pip install django-abc
No matching distribution found for django-mptt==0.5.2,==0.6,==0.6.1 (from django-filer->aldryn-news)
=== Goto git django-filer and look at setup.py ===
New version may already has new version spec.
pip install django-filer --upgrade
==== Headline ====
error: Django 1.8 is installed but Django<1.8,>=1.6 is required by set(['django-cms', 'aldryn-boilerplates'])
==== Error: table does not exist ====
Look if debug-toolbar involves then first disable it from INSTALLED_APPS
===== Django 1.7 Bugs - Migration - Upgrade =====
==== Migration ====
settings.py
MIGRATION_MODULES = {
'cms': 'cms.migrations_django',
'menus': 'menus.migrations_django',
'filer': 'filer.migrations_django',
# Add also the following modules if you're using these plugins:
#'djangocms_file': 'djangocms_file.migrations_django',
#'djangocms_flash': 'djangocms_flash.migrations_django',
#'djangocms_googlemap': 'djangocms_googlemap.migrations_django',
#'djangocms_inherit': 'djangocms_inherit.migrations_django',
#'djangocms_link': 'djangocms_link.migrations_django',
#'djangocms_picture': 'djangocms_picture.migrations_django',
#'djangocms_snippet': 'djangocms_snippet.migrations_django',
#'djangocms_teaser': 'djangocms_teaser.migrations_django',
#'djangocms_video': 'djangocms_video.migrations_django',
'djangocms_text_ckeditor': 'djangocms_text_ckeditor.migrations_django',
#'aldryn_news': 'aldryn_news.migrations_django',
}
./manage.py makemigrations
./manage.py migrate
==== Clean Database ====
Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
=== sqlflush ===
./manage.py sqlflush --help
Returns a list of the SQL statements required to return all tables in the database to the state they were in just after they were installed.
==== Table django_session not exist ====
In MySQL Database delete row 'django_sessions' from the table 'django_migrations'.
delete from django_migrations where id=17;
Delete all migration files in migrations folder.
Try again python manage.py makemigrations and python manage.py migrate command.
==== Headline ====
===== Django 1.8 Bugs - Migration - Upgrade =====
==== Uninstall South ====
There is no South database module 'south.db.mysql' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS.
pip uninstall South
==== MIGRATION_MODULES ====
Django-cms 3.1. migrations by Django is now in migrations and south migration is in south_migrations. so remove the paths in settings.MIGRATION_MODULES.
rm -rf vfoss_env
virtualenv --no-site-packages vfoss_org_env
pip install -r requirement.txt
mysql> drop database [database name]
create database [database name]
./manage.py makemigrations
./manage.py migrate
==== Django Template ====
problem:
from django.template import VariableNode, Variable
ImportError: cannot import name VariableNode
change
from django.template import TemplateSyntaxError, NodeList, VariableNode
to
from django.template.base import TemplateSyntaxError, NodeList, VariableNode
in
vim /mnt/extdata/workspace/70_project_vfoss/vfoss_django_dev_ws/vfoss_org_env/lib/python2.7/site-packages/cms/utils/placeholder.py
vim /mnt/extdata/workspace/70_project_vfoss/vfoss_django_dev_ws/vfoss_org_env/lib/python2.7/site-packages/sekizai/helpers.py
==== django.contrib.formtools ====
Formtools is removed from django 1.8: https://docs.djangoproject.com/en/1.8/releases/1.8/#removal-of-django-contrib-formtools
Install it: https://docs.djangoproject.com/en/1.8/ref/contrib/formtools/
Problem:
File "/mnt/extdata/workspace/70_project_vfoss/vfoss_django_dev_ws/vfoss_org_env/lib/python2.7/site-packages/cms/plugin_pool.py", line 6, in
from django.contrib.formtools.wizard.views import normalize_name
ImportError: No module named formtools.wizard.views
change:
from django.contrib.formtools.wizard.views import WizardView
to:
from formtools.wizard.views import WizardView
===== Headline =====
[[programming:django:django_cms:django_cms_plugin_blog|Django-cms plugin blog]]
[[programming:django:django_cms:django_cms_themes|Django-cms themes]]
[[programming:django:django_cms:django_cms_zinnia|Django-cms Zinnia]]
http://www.ibm.com/developerworks/opensource/library/os-django/index.html
===== Level 2 Headline =====
Show dependency tree:
pip install pipdeptree
pipdeptree -fl
====== Plugin Development ======
https://github.com/alex/django-taggit/issues/185
===== djangocms-blog =====
Add static placeholder:
http://www.pylucid.org/en/blog/2015/02/22/setup-a-enhanced-djangocms-blog-page/
==== PlaceholderField ====
models.py
content = PlaceholderField('post_content', related_name='post_content')
With Django-cms 3.1, this field is edited directly from front-end. Switch to "structure" editing and insert Text-plugin.
===== aldryn-news (not used) =====
**Not used because hvad issues**
Forked https://github.com/aldryn/aldryn-news to github/vfoss-org
==== Prerequsites ====
* django 1.7.8
* django-cms 3.1
==== Add Linked Url to aldryn-news model ====
Enable link to external url.
See cmsplugin-news!
vim aldryn_news/models.py
141 class News(TranslatableModel):
142 THUMBNAIL_SIZE = getattr(settings, 'ALDRYN_NEWS_ITEM_THUMBNAIL_SIZE', '100x100')
143
144 translations = TranslatedFields(
145 title=models.CharField(_('Title'), max_length=255),
146 slug=models.CharField(_('Slug'), max_length=255, blank=True,
147 help_text=_('Auto-generated. Clean it to have it re-created. '
148 'WARNING! Used in the URL. If changed, the URL will change. ')),
149 lead_in=HTMLField(_('Lead-in'),
150 help_text=_('Will be displayed in lists, and at the start of the detail page')),
151 meta={'unique_together': [['slug', 'language_code']]}
152 )
153 key_visual = FilerImageField(verbose_name=_('Key Visual'), blank=True, null=True)
154 content = PlaceholderField('blog_post_content')
155 publication_start = models.DateTimeField(_('Published Since'), default=datetime.datetime.now,
156 help_text=_('Used in the URL. If changed, the URL will change.'))
157 publication_end = models.DateTimeField(_('Published Until'), null=True, blank=True)
158 category = models.ForeignKey(Category, verbose_name=_('Category'), blank=True, null=True,
159 help_text=_('WARNING! Used in the URL. If changed, the URL will change.'))
160 objects = RelatedManager()
161 published = PublishedManager()
162 tags = TaggableManager(blank=True, through=TaggedItem)
163
# Add new field <------------
164 link = models.URLField(_('Link'), blank=True, null=True,
165 help_text=_('This link will be used a absolute url'
166 ))
........
176 def get_absolute_url(self, language=None):
177 language = language or get_current_language()
178 slug = get_slug_in_language(self, language)
179
180 #if self.link:
181 # if not self.content:
# Return URL in the Link
182 return self.link <----------------
183
==== Enable Editing The Link Field ====
vim aldryn_news/admin.py
14 class NewsAdmin(FrontendEditableAdmin, TranslatableAdmin, PlaceholderAdmin):
15
16 list_display = ['__unicode__', 'publication_start', 'publication_end', 'all_translations']
17 form = NewsForm
# Add 'link' <----------------
18 frontend_editable_fields = ('title', 'lead_in', 'link')
19
20 def get_fieldsets(self, request, obj=None):
21 fieldsets = [
22 (None, {'fields': ['title', 'slug', 'category', 'publication_start', 'publication_end']}),
# Add 'link' <----------------
23 (None, {'fields': ['key_visual', 'lead_in', 'link', 'tags']})
24 ]
25
26 # show placeholder field if not CMS 3.0
27 if LooseVersion(cms.__version__) < LooseVersion('3.0'):
28 fieldsets.append(
29 ('Content', {
30 'classes': ['plugin-holder', 'plugin-holder-nopage'],
31 'fields': ['content']}))
32
33 return fieldsets
34
35 admin.site.register(News, NewsAdmin)
==== Edit Template ====
Copy aldryn-news tempate to templates folder and insert bootsnipp.
===== Auto Add Author When Editing Model =====
In Django framework there is a chain of services / component processing request and return response. E.g., Request --> View --> Form --> DB, Response or Request --> ModelAdmin --> Form --> DB, Response. Each component has functions that can be overridden to modify the data object passed through the chain.
* http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-fields/
==== Class Based View ====
* http://stackoverflow.com/questions/5785727/accessing-request-user-in-class-based-generic-view-createview-in-order-to-set-fk
* https://docs.djangoproject.com/en/1.8/topics/class-based-views/generic-editing/
==== ModelAdmin + ModelForm ====
Just modify save_model as shown here:
* http://djangopatterns.com/patterns/models/automatically_filling_user/
===== Adding Social Authentication =====
Trying this: https://github.com/omab/python-social-auth#installation
====== Error & solution ======
==== Clear database ====
python manage.py syndb
if using south
python manage.py migrate
For initial update
python manage.py syncdb --all
python manage.py migrate --fake
==== Thumbnail processor ====
Install thumbnail app:
pip install easy_thumbnails
or
pip install sorl-thumbnail
Edit settings.py
162 # filer
163 THUMBNAIL_PROCESSORS = (
164 'easy_thumbnails.processors.colorspace',
165 'easy_thumbnails.processors.autocrop',
166 #'easy_thumbnails.processors.scale_and_crop',
167 'filer.thumbnail_processors.scale_and_crop_with_subject_location',
168 'easy_thumbnails.processors.filters',
169 )
==== js & css files under /static/ can't be found ====
Problem:
Cause: django dev-enviroment doesn't serve static file. Before deployment `python manage.py collectstatic` must be run so that all static files are copied to "settings.STATIC_ROOT" by python-staticfiles. See python-staticfiles documentation.
Solution: move all static files to 'static-dev' and add 'os.path.join(PROJECT_PATH, "static-dev/jquery/"),' to 'settings.STATICFILES_DIRS'.
=== When deploy always run collectstatic ===
This will also copy static files from themes, template to static dir. Important.
==== settings.py SITE_ID ====
When add new site, change SITE_ID=2 so localhost use it.
Multi subdomain mit site
I tried to change code in django-cms/cms/views.py and menu.py
from django.conf import settings
class CheckSubdomain(object):
def process_request(self, request):
if settings.DEBUG: return
sites = Site.objects.filter(domain=request.get_host())
if len(sites): settings.SITE_ID = sites[0].pk