레이블이 django인 게시물을 표시합니다. 모든 게시물 표시
레이블이 django인 게시물을 표시합니다. 모든 게시물 표시

12/30/2010

set image path in django template

add the below to urls.py


site_media = os.path.join( os.path.dirname(__file__), 'site_media' )


in urlpatterns, add this


(r'^site_media/(?P.*)$', 'django.views.static.serve', { 'document_root': site_media } ),


All images or something should be in site_media folder.

In a template file, insert a tag like



12/22/2010

enabling 한글 address in http django..

When 한글 address are given to django, ascii error occurs .

Solution :


$ cat /usr/local/lib/python2.6/site-packages/sitecustomize.py
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
ref1: http://bbs.python.or.kr/viewtopic.php?p=64687&highlight=&sid=cee81191b135b182f8f28a25b37d42f7
ref2: http://blog.codeguruz.com/tag/python

Then,
$ vi /usr/lib/pymodules/python2.6/django/http/__init__.py
goto line 309 (or something..)
def _convert_to_ascii(self, *values):
"""Converts all values to ascii strings."""
for value in values:
if isinstance(value, unicode):
try:
#value = value.encode('us-ascii') ## remove this line
value = value.encode('utf-8') ## add this
except UnicodeError, e:
e.reason += ', HTTP response headers must be in US-ASCII format'
raise
else:
value = str(value)
if '\n' in value or '\r' in value:
raise BadHeaderError("Header values can't contain newlines (got %r)" % (value))
yield value

12/03/2010

set mysql charset to utf8 in ubuntu 10.04

1. MySQL setting

Edit my.cnf with this command :
$ sudo vi /etc/mysql/my.cnf

in [client] section, add this :

default-character-set = utf8

and, in [mysqld] section, add these :

default-character-set=utf8
default-collation=utf8_general_ci
init_connect=set collation_connection=utf8_general_ci
init_connect=set names utf8
character-set-server=utf8
collation-server=utf8_general_ci
character-set-client-handshake = TRUE

The restart mysqld

$ sudo /etc/init.d/mysql restart



Confirm :

mysql> show variables like 'char%'
-> ;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

* IMPORTANT : Create database with "defalut charset=utf8" option.
user@myserver:/home/user$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 56
Server version: 5.1.41-3ubuntu12.7 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database DBNAME default charset=utf8;

Query OK, 1 row affected (0.00 sec)

mysql> Bye

2. Django settting

in settings.py add this :

LANGUAGE_CODE = 'ko-KR'
./managy.py syncdb should be run.


Then, 한글 입력 is ok in Django application.

11/15/2010

django + apache server 1

1. Set apache to read my django project

cat /etc/apache2/httpd.conf

(location "/")
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['[path.to.django.project]', '[path.to.other.location.which.has.python.code]'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE [projectName].settings
PythonDebug On
(/location)

where, "(" and ")" should be replaced with "<" and ">"

In web browser, http://localhost/ gives the "It worked" page of django.
If "import error" occur, check the [path.to.directory] point out the right directory which contains the module.

2. utf-8 setting

$ cat /etc/apache2/envvars

...
export LANG=ko_KR.UTF-8
export LC_ALL=ko_KR.UTF-8
...

$ /etc/init.d/apache2 restart


3. Running a development server with apache mod_python

If a django python code was changed, apache server must be restarted.
To avoid this, just set

MaxRequestsPerChild 1

in /etc/apache2/apache2.conf

This force apache to reload everything for each request.


When debugging, use the code below to display a debugging information.

assert False, 'debug text'


4. Make database (mysql)


5. Make project, app

django-admin startproject (aaa)
cd aaa
./manage.py startapp (bbb)


in settings.py

add the code below

import os
ROOT_PATH = os.path.dirname(__file__)

add to TEMPLATE_DIRS

ROOT_PATH + '/templates',


and in INSTALLED_APPS, add

'aaa.bbb',


change the LANGUAGE_CODE option like

LANGUAGE_CODE = 'ko-KR'


and setting DB infomation.

DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'dbname' # Or path to database file if using sqlite3.
DATABASE_USER = 'userid' # Not used with sqlite3.
DATABASE_PASSWORD = 'password' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.


6. sync db

manage.py syncdb

Here, set user name and password.


7.