2009年2月5日木曜日

Django DBを使用するための設定

DBを使用するための設定

前回のプロジェクト名のフォルダmyprojectの中の
settings.py  ファイルを編集します。

1-1.MySQL を使用する場合

MySQLでDB test1 を作成しておきます。
そのDBにユーザ、パスワードを作成しておきます。
必要があれば、MySQL-python-1.2.2.win32-py2.5.exeをインストール

settings.py  ファイルを


DATABASE_ENGINE = 'mysql'      
DATABASE_NAME = 'test1'        
DATABASE_USER = 'test1'        
DATABASE_PASSWORD = 'passwd'    
DATABASE_HOST = 'localhost'    
DATABASE_PORT = '3306'         



INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'myproject.myapp',
)


1-2.SQLite3 を使用する場合

settings.py  ファイルを


DATABASE_ENGINE = 'sqlite3'    
DATABASE_NAME = 'test1.dat'     #DB file name
DATABASE_USER = ''            
DATABASE_PASSWORD = ''        
DATABASE_HOST = ''            
DATABASE_PORT = ''            



INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'myproject.myapp',
)

test1.datというDBファイルを作成する設定です。



1-3.PostgreSQL を使用する場合

PostgreSQLでDB test1 を作成しておきます。
そのDBにユーザ、パスワードを作成しておきます。

psycopg2-2.0.8.win32-py2.5-pg8.3.4-release.exe
をインストールしておきます。(バージョンは使用環境に合わせて)

settings.py  ファイルを


DATABASE_ENGINE = 'postgresql_psycopg2' 
DATABASE_NAME = 'test1'        
DATABASE_USER = 'test1'           
DATABASE_PASSWORD = 'passwd'         
DATABASE_HOST = 'localhost'          
DATABASE_PORT = '5432'            



INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'myproject.myapp',
)

あとそれぞれに、タイムゾーン、言語環境を設定
TIME_ZONE         = 'Japan/Tokyo' 
LANGUAGE_CODE     = 'ja-jp' 

各INSTALLED_APPSに
'myproject.myapp',とmodels.pyの位置を指定しておきます。

--------------

2.各テーブルの作成

プロジェクト名のフォルダの下のアプリケーション名のフォルダにある
models.pyを

from django.db import models

# Create your models here.

class Post(models.Model): #
    title = models.CharField(max_length=100)
    entry = models.CharField(max_length=400)
    updated_at = models.DateTimeField('date published')

と記載し

プロジェクト名のフォルダmyprojectで

myproject>manage.py syncdb

Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table myapp_post

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username: ユーザ名
E-mail address: メールアドレス
Password:パスワード
Password (again):パスワード
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model

でDBに各テーブルが作成されます。


2009年1月25日日曜日

Djangoプロジェクト作成

Djangoプロジェクト作成
(Djangoプロジェクト作成したときのメモ)

環境変数のpathに C:¥Python25;C:¥Python25¥Scripts;
を追加(Python、DjangoがC:¥Python25にインストール済みの場合)


手順:Djangoプロジェクトの作成->アプリケーションの作成

workフォルダ
       |-----プロジェクト名のフォルダ
                  |-----アプリケーション名のフォルダ
       
1.Djangoプロジェクトの作成

コマンドプロンプトで、作業フォルダーで
django-admin.py startproject プロジェクト名

と実行。

で、プロジェクト名のフォルダが作成され中に、
__init__.py
manage.py
settings.py
urls.py

のファイルが作成されます。

2.アプリケーションの作成

コマンドプロンプトで、
プロジェクト名フォルダーに移動(cd プロジェクト名)し

manage.py startapp アプリケーション名

と実行。

で、アプリケーション名のフォルダが作成され中に、
__init__.py
views.py
models.py

のファイルが作成されます。

2008年12月5日金曜日

Data

・GAEデータのModel定義
(SQLのテーブル?、Bean?、構造体?)

#
# Model
#
class Part(db.Model):
  noint = db.IntegerProperty()     #int&long
  mojistr = db.StringProperty()    #String
  nofloat = db.IntegerProperty()    #float
  contentstr = db.StringProperty(multiline=True)
  author = db.UserProperty()
  date = db.DateTimeProperty(auto_now_add=True)
  day = db.DateProperty()

・データSET

class Sql1insert(webapp.RequestHandler):
  def post(self):
   part = Part()
   part.noint =  int(self.request.get('nostr'))    # int

   part.put()

2008年11月30日日曜日

GAE

GAEちょっとMeno。

開発環境では
today = datetime.today()
wee = ('月', '火', '水', '木', '金', '土', '日')[today.weekday()]
でOK。
サーバーでは'月', '火', '水', '木', '金', '土', '日'、NGでサーバーエラー。。。。。
today = datetime.today()
wee = ('mon','tue','wed','thu','fri','sat','sun')[today.weekday()]
でOK。
コメントの日本語や入力値の日本語処理は??(UTF-8で)。
追記:コメントでもServerError?(***.py  ファイル)。
追記:コードの先頭に 「#  coding: utf-8」 を記載することでコード内の日本語OK。
wee = ('月', '火', '水', '木', '金', '土', '日')[today.weekday()] もOKに。
追記:データ入力値の日本語OK。
(コードをUTF-8で保存を忘れないように。。。)

html内の日本語OK。

ロケールがcしかないみたい。(開発環境でも)

日本時間は、UTCに9時間+
today = datetime.today()
    jikanutc = today.strftime("%Y-%m-%d %H:%M")
    jikanja = ((datetime.utcnow() + timedelta(hours=9)).strftime("%Y-%m-%d %H:%M"))