Django CMS step by step

1. 准备工作:

sudo apt-get install python-setuptools # This is for easy_install
sudo easy_install django # 安装Django 1.2x如果直接采用sudo apt-get install python-django则安装的是1.1x版本,这个版本不能与Django CMS一起使用,因为缺少CSRF模块
sudo easy_install django-cms
# create the project
django-admin startproject demo # demo is your project name.

2. 修改配置文件:
参考:
Django-CMS在开发服务器下配置
注意将你的模板文件目录加进来。

3. 修改url route文件:
参考:
Django-CMS在开发服务器下配置

4. 启用数据库

./manage.py syncdb

5. 创建你在配置文件中指定的模板文件
刚才在settings.py中指定的几个模板,比如base.html之类是不存在的。而且也没有缺省的可以用。所以你需要手工编辑一下。

6. 开始测试吧:

./manage.py runserver

说明:
1. 按照Django-CMS和 Django-CMS在开发服务器下配置一文的内容配置,基本上不会遇到什么问题。
2. 《Django Web开发指南》这本小册子显然没有《Web开发敏捷之道》写得清晰自然。python和ruby的用户群确实可以看出点点区别。
3. Django的版本是本次测试折磨人的要点。csrf_token…:(
4. Ubuntu下还没有遇到:不写PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))不可用的问题。

参考文件:
1. Django-CMS在开发服务器下配置
2. Django CMS Installation Guide
3. No module named csrf
4.

Django使用frameset

前兩天想用Django的時候,因為手頭這個東西是用到frame,於是有點悲哀了。後來看到這個已經解決的文章,發現自己變蠢的時間最近多了。
將各frame的src映射即可了。。。

Django的幾篇而意義的探討:
1. 混合使用django模板和jinja模板
2. 偶对django的rant
3. 初识django, 一起进入python的世界吧
4.

Django模板中使用css, javascript

[sourcecode language=”python”]
(r’^css/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/var/www/django-demo/css’}),
(r’^js/(?P
.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/var/www/django-demo/js’}),
(r’^images/(?P
.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/var/www/django-demo/images’}),
[/sourcecode]

模板中使用下述方式即可:
[sourcecode language=”html”] [/sourcecode]

注:可采用os.path.dirname(globals()[“__file__”])来获得当前文件所在路径,比如
[sourcecode language=”python”]
(r’^css/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: os.path.dirname(globals()[“__file__”])+’/css’}),
[/sourcecode]
可以使用os.path.abspath()函数返回此路径的绝对路径。

The Definitive Guide to Django 2nd Edition-Using the Template System

Let’s dive into Django’s template system so you can see how it works—but we’re not yet going to integrate it with the views that we created in the previous chapter. Our goal here is to show you how the system works independent of the rest of Django. (Usually you’ll use the template system within a Django view, but we want to make it clear that the template system is just a Python library that you can use anywhere, not just in Django views.)
Here is the most basic way you can use Django’s template system in Python code:
1. Create a Template object by providing the raw template code as a string.
2. Call the render() method of the Template object with a given set of variables (the con-
text). This returns a fully rendered template as a string, with all of the variables and
template tags evaluated according to the context.

In code, it looks like this:
[sourcecode language=”python”]
>>> from django import template
>>> t = template.Template(‘My name is {{ name }}.’)
>>> c = template.Context({‘name’: ‘Adrian’})
>>> print t.render(c)
My name is Adrian.
>>> c = template.Context({‘name’: ‘Fred’})
>>> print t.render(c)
My name is Fred.
[/sourcecode]
The following sections describe these steps in much more detail.

做实验是最爽的。一边看着AstroBoy,一边测试这些指令,那边厢Toby已经因为玩耍归于尘土。
而完全按照书里的语句提示:
[sourcecode language=”python”]
u@s:/path/to/django-project$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from django import template
>>> t = template.Template(“My name is {{ name }}.”)
Traceback (most recent call last):
File ““, line 1, in
File “/var/lib/python-support/python2.6/django/template/__init__.py”, line 164, in __init__
if settings.TEMPLATE_DEBUG and origin is None:
File “/var/lib/python-support/python2.6/django/conf/__init__.py”, line 28, in __getattr__
self._import_settings()
File “/var/lib/python-support/python2.6/django/conf/__init__.py”, line 57, in _import_settings
raise ImportError(“Settings cannot be imported, because environment variable %s is undefined.” % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
>>>
[/sourcecode]
这是因为Django的template要与项目相关,所以要使用python manage.py shell交互测试才可以。那边阿童木要活了。BlueCore is input….
[sourcecode language=”python”]
u@s:/path/to/django-project$ python ./manage.py shell
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
(InteractiveConsole)
>>> from django import template
>>> t = template.Template(“My name is {{ name }}.”)
>>> c = template.Context({“name”: “Hello”})
>>> print t.render(c)
My name is Hello.
>>>
[/sourcecode]

LAMP平台之Ubuntu Server加AMP

昨天的MySQL安装是序曲,实质是安装LAMP的一个环节。在Ubuntu的世界里apt-get可以解决大部分的事。当然源码方式更优,但是现在还不到深入研究的时间。

./configure & make & make install 三部曲唱起来容易,唱好更难了。

使用apt-get方式在Ubuntu Server里安装AMP,步骤如下:

sudo apt-get install apache2 php5 mysql-server mysql-client php5-mysql php5-gd phpmyadmin

全部执行完毕(MySQL环节见昨天的日志),访问

http://xxx.xxx.xxx.xxx/phpmyadmin/

就可以了。真是简单快捷,居家调试LAMP程序必备呀。

索性再增加个python支持:

sudo apt-get install python libapache2-mod-python python-django

注:

phpmyadmin安装目录在/usr/share/phpmyadmin/