(转)Django ====> 实战学习篇三 MODEL之外需要知道的
####django的scaffold:
相对于rails的scaffold,即在model的基础上,自动生成crud界面。django 引入了插件djang-groundwork,并做简单的配置即可使用。扩展了manage.py命令
$git clone https://github.com/madhusudancs/django-groundwork.git $ls django-groundwork AUTHORS LICENSE README.rst django-groundwork
将其中的django-groundwork复制到project当中,在setting.py中加入该app
安装后,使用$python manage.py help可以看到,列出的可用命令中多了一个groundwork。其语法是:
$python manage.py groundwork appname ModelName1 ModelName2
接下来使用这个app为Product生成scaffold:
$python manage.py groundwork depotapp Product,就会生成所谓的scaffold。
此时运行开发服务器(python manage.py runserver),就可以访问下面的地址:
http://localhost:8000/depotapp/product/list/ 访问Product列表,并链接到create,edit,view等界面。
#### django-groundwork 生成物分析
其实其生成物无非就是URLconf+MTV!!!
先来看URLconf:
如果你的django版本是1.4以上的,那么需要手动修改一些文件,如下: 删除与manage.py同层的urls.py 和 setting.py 但是不删除templtes目录 查看depot中的urls.py 并加入以下内容: urlpatterns += patterns('', url(r'^depotapp/', include('depotapp.urls')), ) 进入depotapp目录下查看自动生的urls.py如下: from django.conf.urls.defaults import * from models import * from views import * urlpatterns = patterns('', (r'product/create/$', create_product), (r'product/list/$', list_product ), (r'product/edit/(?P<id>[^/]+)/$', edit_product), (r'product/view/(?P<id>[^/]+)/$', view_product), )
继续查看MTV
M不用说了,看T,在templates目录下自动生成了 模型文件 [shishang@shishang templates]$ ls base.html depotapp [shishang@shishang templates]$ ls depotapp/ create_product.html edit_product.html list_product.html view_product.html [shishang@shishang templates]$ pwd /home/shishang/depot/templates 查看depotapp中的views文件如下: 4 from django import forms 5 from django.template import RequestContext 6 from django.http import HttpResponse, HttpResponseRedirect 7 from django.template.loader import get_template 8 from django.core.paginator import Paginator 9 from django.core.urlresolvers import reverse 10 11 # app specific files 12 13 from models import * 14 from forms import * 15 16 17 def create_product(request): 18 form = ProductForm(request.POST or None) 19 if form.is_valid(): 20 form.save() 21 form = ProductForm() 22 23 t = get_template('depotapp/create_product.html') 24 c = RequestContext(request,locals()) 25 return HttpResponse(t.render(c)) 26 27 28 29 def list_product(request): 30 31 list_items = Product.objects.all() 32 paginator = Paginator(list_items ,10) 33 34 35 try: 36 page = int(request.GET.get('page', '1')) 37 except ValueError: 38 page = 1 39 40 try: 41 list_items = paginator.page(page) 42 except : 43 list_items = paginator.page(paginator.num_pages) 44 45 t = get_template('depotapp/list_product.html') 46 c = RequestContext(request,locals()) 47 return HttpResponse(t.render(c)) 48 49 50 51 def view_product(request, id): 52 product_instance = Product.objects.get(id = id) 53 54 t=get_template('depotapp/view_product.html') 55 c=RequestContext(request,locals()) 56 return HttpResponse(t.render(c)) 57 58 def edit_product(request, id): 59 60 product_instance = Product.objects.get(id=id) 61 62 form = ProductForm(request.POST or None, instance = product_instance) 63 64 if form.is_valid(): 65 form.save() 66 67 t=get_template('depotapp/edit_product.html') 68 c=RequestContext(request,locals()) 69 return HttpResponse(t.render(c))
视图的内容比较多,主要是模板,其次还有模型类,form表单等等,基本涵盖了典型的web交互应用内容。
depotapp中forms.py内容 1 2 from django import forms 3 from models import * 4 5 6 7 class ProductForm(forms.ModelForm): 8 9 class Meta: 10 model = Product 11 # exclude = [] # uncomment this line and specify any field to exclude it from the form 12 13 def __init__(self, *args, **kwargs): 14 super(ProductForm, self).__init__(*args, **kwargs) 15
其实初学的时候建议,将scaffold这块自动生成的代码内容自己编写一遍,加深一下理解。
“某度”空间 http://hi.baidu.com/new/wenjiashe521
cnblogs空间 http://www.cnblogs.com/wenjiashe521/