顯示具有 Django 標籤的文章。 顯示所有文章
顯示具有 Django 標籤的文章。 顯示所有文章

2011年3月25日 星期五

[Django] Detect Django Version - 取得Django版本

$ pip freeze | grep 'Django'

Django==1.5.1

Or

$ python

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 3, 0, 'final', 0)

Or

python -c "import django; print(django.get_version())"

2011年1月3日 星期一

[Django] Deploy Django on Cherokee Web Server by uWSGI

Cherokee Web Server

The Fastest free Web Server out there!
Cherokee is a very fast, flexible and easy to configure Web Server. It supports the widespread technologies nowadays: FastCGI, SCGI, PHP, CGI, uWSGI, SSI, TLS and SSL encrypted connections, Virtual hosts, Authentication, on the fly encoding, Load Balancing, Apache compatible log files, Data Base Balancing, Reverse HTTP Proxy, Traffic Shaper, Video Streaming and much more.

Cherokee-Admin, a user friendly interface, provides a no-hassle configuration of the server. Check out the benchmarks and documentation to learn more, join our active Community and give it a try to squeeze your hardware to the fullest!

2010年12月29日 星期三

[Django] Don't use field_id to named your field 別用_id命名資料庫爛位

class Car(models.Model):
    manufacturer = models.ForeignKey('production.Manufacturer')

Django 會自動加個 manufacturer_id 所以你不用自己去處理他
如果自己定義一個 xxx_id Django會confuse
除非你想自己處理SQL

官網原文

DATABASE REPRESENTATION
Behind the scenes, Django appends "_id" to the field name to create its database column name. In the above example, the database table for the Car model will have a manufacturer_id column. (You can change this explicitly by specifying db_column) However, your code should never have to deal with the database column name, unless you write custom SQL. You'll always deal with the field names of your model object.


See Also
http://docs.djangoproject.com/en/dev/ref/models/fields/#database-representation

2010年12月28日 星期二

[Django] Model中的null 和 blank 設定與差異



models.CharField(max_length=200, null=False, blank=True)

Django的驗證, 預設每個攔位都是為必要的(required)

所以如果你想要將欄位設為空值, 必須再另外設定

在Django的Field有兩種值可以設定: blank and null

blank 只是用於Django自己的input欄位驗證 (Django’s input validation)

Null 是否允許讓你在Database裡插入NULL, 就是空值 (whether the database accepted NULL)

Null = Python的None = 不存在

blank = True 欄位檢查允許為空值
blank = False 欄位檢查不允許為空值
null = True 寫入資料庫允許為NULL (Python 為 None)
null = False 寫入資料庫不允許為NULL (Python 為 None)

See Also:
http://www.b-list.org/weblog/2006/jun/28/django-tips-difference-between-blank-and-null/

2010年12月27日 星期一

[Django] list all Session

for k in request.session.keys():
    print ' key = ', k
    print request.session.__getitem__(k)

See Also:
django - How to use sessions

2010年12月13日 星期一

[Django] Getting Started part 2 - 設置與同步資料庫

Step 1: 編輯setting.py

設置DATABASE的ENGINE and NAME, 在這我用sqlite

such as:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mysite.sqlite',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}


Step 2: 同步資料庫

python manage.py syncdb

syncdb會根據setting.py裡INSTALLED_APPS的內容來建立需要的資料庫


你可以透過SQLite Manager來查看建立了哪些資料表
http://code.google.com/p/sqlite-manager/

2010年12月7日 星期二

[Django] Getting Started part 1 -建立專案和應用程式

在開始之前, 你的系統必須安裝好Python及Django

我的作業系統是Ubuntu, 所以如果你是用Fedoa或其他系統, 你得用其他方式來安裝
My OS is Ubuntu, so if you use Fedora or CentOS, maybe you have to use yum install to do.

Step 1. Install Python 安裝Python
# apt-get install python


Step 2. Install Django 安裝Django
# apt-get install python-django

你可以試著執行python, 然後打入以下語法來測試是否成功安裝Django
Then you can try to see Django version from above command line:

# python
>>> import django
>>> print django.get_version()
1.2.3


Related Posts Plugin for WordPress, Blogger...