初识django

1.安装
 tar xzvf Django-1.4.3.tar.gz
 cd Django-1.4.3
 sudo python setup.py install
 
2.创建项目
 django-admin.py startproject mysite

3.安装pip的方法:
  sudo apt-get install python-pip python-dev build-essential libpq-dev
  sudo pip install --upgrade pip
  sudo pip install --upgrade virtualenv

4.Django
  __init__.py:让Python把该目录当成一个开发包(即一组模块)所需的文件。这是一个空文件,一般你不需要修改它
  manage.py:一种命令行工具,可让你以多种方式与该Django项目进行交互。 键入python manage.py help
  settings.py:该Django项目的设置或配置。查看并理解这个文件中可用的设置类型及其默认值
  urls.py:django项目的URL设置。可视其为你的django网站的目录。目前,它是空的
  wsgi.py: An entry-point for WSGI-compatible webservers to serve your project. See How to deploy with WSGI for more details.

5.启动python manage.py runserver

6.启动交互解释器:python manage.py shell
 创建模板对象:
  ...

7.启动数据库:
  settings.py:
    编辑DATABASE={...}
    设置TIME_ZONE='Asia/Shanghai'
       LANGUAGE_CODE='zh-cn'
  数据库测试:
    python manage.py shell
    from django.db import connection
    cursor = connection.cursor()

8.创建django app
 python manage.py startapp products

9.校验模型的有效性:
 python manage.py validate

10.安装app
 settings.py - INSTALL_APPS=(...'products',...)

11.生成SQL语句:
 python manage.py sqlall products

12.同步模型到数据库
 python manage.py syncdb

13.SQL客户端
 python manage.py dbshell
 显示创建的表:\dt(PostgreSQL),SHOW TABLES(MySQL),.schema(SQLite)

14.重写模型__unicode__方法

15.后台管理
 设置settings.py:
   INSTALLED_APPS:
     'django.contrib.admin',
   MIDDLEWARE_CLASSES
    'django.middleware.locale.LocaleMiddleware'#国际化本地语言-SessionMiddleware之后
 设置urls.py:
   from django.contrib import admin
   admin.autodiscover()
   url(r'^admin/', include(admin.site.urls)),
 运行:python manage.py syncdb
     python manage.py createsuperuser#创建一个admin的用户帐号
 将模型加入到Admin管理:
   创建:products/admin.py:
       from django.contrib import admin
       from products.models import Author,Book
       admin.site.register(Author)
       admin.site.register(Book)
 运行python manage.py runserver
 http://127.0.0.1:8000/admin/

16.自定义admin表单
  products/admin.py:
    class AuthorAdmin(admin.ModelAdmin):
      fields=['name','age']
    admin.site.register(Author,AuthorAdmin)

17.中文处理
 ...
 每个文件首加:
  # _*_ coding:utf-8 _*_
 ...

18.csrf 403

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

 from django.template import RequestContext
 ...
def add(request):
 if request.method=='POST':
  form=AuthorForm(request.POST)
  if form.is_valid():
    author=form.cleaned_data
    return HttpResponseRedirect('/products/')
 else:
  form=AuthorForm(initial={'name':'输入姓名','age':'输入年龄'})
 return render_to_response('author.html',RequestContext(request,{'form':form}))

 

<form action="" method="post">{%csrf_token%}
       <table>
         {{form.as_table}}
       </table>
       <input type="submit" value="add"/>
</form>

 

 

posted @ 2013-01-09 09:19  WenEric  阅读(281)  评论(0编辑  收藏  举报