py Django 尝试

py Django 尝试


环境
  py2.5
  Django 
  eclipes + py 插件
  ant + 自定义build
  MySQLdb

参考:
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut06.html
 

pybuild.properties
  project.name=myTurtle

<? xml version="1.0" ?>
< project  default =""  basedir ="." >
    
< property  file ="pybuild.properties"   />
    
< target  name ="startproject" >
        
< exec  executable ="cmd" >
            
< arg  value ="/c"   />
            
< arg  value ="django-admin.py startproject "   />
            
< arg  value ="${project.name}"   />
        
</ exec >
    
</ target >

    
< target  name ="buildCProject" >
        
< input  addproperty ="newProject"  message ="准备新建子项目名称>>"   />
        
< exec  executable ="cmd" >
            
< arg  value ="/c"   />
            
< arg  value =" python ./${project.name}/manage.py startapp ${newProject}"   />
        
</ exec >
    
</ target >


    
< target  name ="run" >
        
< exec  executable ="cmd" >
            
< arg  value ="/c"   />
            
< arg  value ="start python ./${project.name}/manage.py runserver"   />
        
</ exec >
    
</ target >

    
< target  name ="end" >
        
< exec  executable ="cmd" >
            
< arg  value ="/c"   />
            
< arg  value ="tskill python"   />
        
</ exec >
    
</ target >

    
    
< target  name ="db_init" >
        
< exec  executable ="cmd" >
            
< arg  value ="/c"   />
            
< arg  value =" python ./${project.name}/manage.py syncdb"   />
        
</ exec >
    
</ target >
</ project >



$乱码问题
settings.py
LANGUAGE_CODE 
=   ' zh-cn '
TIME_ZONE 
=   ' Asia/Shanghai '
request.encoding
= ' utf8 '  


$python 运行本的命令得到返回参数: 
    fp
= os.popen(  " dir  " ,    " " )
    x
= fp.read() 


$挂载资源
  urls.py patterns 
+   (r ' ^$ ' ' test.test.index ' ),


$页面返回
from  django.http  import  HttpResponse
def  index(request):
    
return  HttpResponse( " Hello, Django. " )


$取得html访问值
  
if  request.POST.has_key( ' a ' )
  int(request.POST[
' a ' ])

$页面编码设置,文件第一行加入
  
# coding=UTF-8


$使用模板
  settings.py TEMPLATE_DIRS  
+   ' ./templates ' # 模板路径
   # 方法1
    from  django.shortcuts  import  render_to_response
   
def  index(request):
       
return  render_to_response( ' list.html ' , { ' address ' : address})
  
# templates/list.html
      < h2 > 通讯录 </ h2 >
     
< table border = " 1 " >
     
< tr >< th > 姓名 </ th >< th > 地址 </ th ></ tr >
     {
%   for  user  in  address  % }
     
< tr >
     
< td > {{ user.name }} </ td >
     
< td > {{ user.address }} </ td >
     
</ tr >
     {
%  endfor  % }
    
</ table >
  
# 方法2
       from  django.http  import  HttpResponse
    
from  django.template  import  loader, Context
    response 
=  HttpResponse(mimetype = ' text/csv ' )
    response[
' Content-Disposition ' =   ' attachment; filename=%s.csv '   %  filename
    t 
=  loader.get_template( ' csv.html ' )
    c 
=  Context({ ' data ' : address,})
    response.write(t.render(c))
    
return  response
     
# HTML
    { %   for  row  in  data  % } " {{ row.0|addslashes}} " " {{ row.1|addslashes}} " ,{ %  endfor  % }
    
   
$提供下载
  
from  django.template  import  loader, Context
  
# 设置返回下载属性
  response  =  HttpResponse(mimetype = ' text/csv ' )
  response[
' Content-Disposition ' =   ' attachment; filename=%s.csv '   %  filename

$使用session和数据库
    http:
// www.woodpecker.org.cn / obp / django / django - stepbystep / newtest / doc / tut05.html

$新建子项目
  settiongs.py 
+  INSTALLED_APPS  =   ' 总包名.新建包名 ' ,
  manage.py startapp 包名


$表链关系
http://www.woodpecker.org.cn/obp/django/django-faq/model-api.html
   
   






整理 www.blogjava.net/Good-Game

你可能感兴趣的:(py Django 尝试)