Django创建hello world

创建app:

./manage.py startapp hello_world

修改自动生成的子目录下的views.py:

from django.http import HttpResponse

def hello_world(request) :

return HttpResponse(‘Hello World’)

修改app上级目录(即项目文件夹)下的urls.py

from django.conf.urls.defaults import *

**from hello.hello_world.views import hello_world**

# Uncomment the next two lines to enable the admin:

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns(”,

**(‘^hello_word/$’,hello_world), **

(r’^blog/’,include(‘hello.blog.urls’)),

# Example:

# (r’^hello/’, include(‘hello.foo.urls’)),

# Uncomment the admin/doc line below to enable admin documentation:

# (r’^admin/doc/’, include(‘django.contrib.admindocs.urls’)),

# Uncomment the next line to enable the admin:

(r’^admin/’, include(admin.site.urls)),

)

附本人rst 格式的笔记

接主文:
===============================================
    1. 运行开发服务器::
    
        $ ./manage.py runserver 
    
    2. 新建app::
        
            Administrator@PC-201107130118 ~/mydjango/hello
            $ ./manage.py startapp blog
        
        自动建立的文件
            Administrator@PC-201107130118 ~/mydjango/hello
            $ ls -la blog/
            总用量 96
            drwxr-xr-x 1 Administrator None   0 五月  4 14:46 .
            drwxr-xr-x 1 Administrator None   0 四月  9 22:36 ..
            -rw-r--r-- 1 Administrator None   0 五月  4 14:46 __init__.py
            -rw-r--r-- 1 Administrator None  57 五月  4 14:46 models.py
            -rw-r--r-- 1 Administrator None 514 五月  4 14:46 tests.py
            -rw-r--r-- 1 Administrator None  26 五月  4 14:46 views.py
        
        这时编辑settings.py,把该app加入到INSTALLED_APPS ::
        
            INSTALLED_APPS = (
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django.contrib.sessions',
                'django.contrib.sites',
                'django.contrib.messages',
                'staticfiles',
                # Uncomment the next line to enable the admin:
                'django.contrib.admin',
                # Uncomment the next line to enable admin documentation:
                # 'django.contrib.admindocs',
                'hello.blog',
            )
            
    3. 设计Model
        
        编辑blog下的models.py::
        
            from django.db import models

            # Create your models here.

            class BlogPost(models.Model):
                title = models.CharField(max_length=150)
                body = models.TextField()
                timestamp = models.DateTimeField()

        加入 了3个字段,实际上django还会增加一个自增的id字段
        
    4. 执行数据库同步( syncdb )::
    
            Administrator@PC-201107130118 ~/mydjango/hello
            $ ./manage.py syncdb
            Creating table blog_blogpost
            No fixtures found.

        当你执行syncdb命令时,Django会查找INSTALLED_APPS里的每一个models.py文件,并为找到的每一个model都创建一张数据库表。可以写到postinstall 里每次push 到dotcloud时执行。
        
    .. note::

        在使用某些app如 django.contrib.auth 时会问一些问题,但在前面我们使用了postinstall 调用 mkadmin.py手动创建了,这里也就无需这一步。


    5.  注册BlogPost到admin,导入admin应用,并在models.py最后加入一句,代码如下 ::
        
            from django.db import models
            from django.contrib import admin

            # Create your models here.

            class BlogPost(models.Model):
                title = models.CharField(max_length=150)
                body = models.TextField()
                timestamp = models.DateTimeField()

                
            admin.site.register(BlogPost)

        这时在登录后台可以到BlogPost进行插入数据。
        

.. note:: 

    插入中文时会出错,编码问题。修改mysql 数据库的默认编码为uft8. ::
    
        用SSH连接到数据库主机:执行mysql客户端,当然也可以在本地直接用root帐号连接 ,执行以下语句 
    
        mysql> ALTER DATABASE hello CHARACTER SET utf8 COLLATE utf8_general_ci;
    
        mysql> DROP TABLE IF EXISTS blog_blogpost;
        
        mysql> show variables like 'character_set_%';
        
        mysql> show variables like 'collation_%';
    
    再执行一次syncdb即可
    

.. rubric:

    参考文章 : https://docs.djangoproject.com/en/1.3/ref/unicode/
    
    MySQL编码设置:http://dev.mysql.com/doc/refman/5.1/en/charset-database.html
    
    
    
    
- EOF -