Django Web开发环境的搭建(二)

来源:互联网 发布:男生衣服品牌知乎 编辑:程序博客网 时间:2024/05/22 15:47

本博文根据python2.7.3、Django1.7.1,运行环境为linux Ubuntu12.04,当然开始学习django是在win7 python2.7.8 Django1.7下搭建,后来发现在linux下开发可以省去一些default设置的修改,就转战linux学习。该博文针对win7,这一篇开始将细讲linux环境下开发

linux下django开发环境的搭建和windows下环境搭建类似,在命令下有一点出入,例如templates文件夹在win7下是得放在工程目录下(不太记得了),而linux得放在app目录下。二话不说,上图比说的更有说服力。

1、目录结构

mytest1为project,hello为app

django-admin startproject mytest1

django-admin startapp hello

 

2、部署完后可以开始启动服务器了

当然可能会出现启动不成功的情况,在win7下也可能有遇到过,执行如下操作即可

3、说说view.py中怎样使用模板吧

 

①from django.template import loader,Context

......

t=loader.get_template(‘index.html’) #  等于 t=template.Template(‘str’)

c=Context({})

html=t.render(c)

return HttpResponse(html)

②from django.shortcuts import render_to_response

......

return render_to_response(‘gujiawei.html’,{})

在稍后的文章中会专门浅谈利用模板与python交互的例子。

4、多对多映射小记

 

author.objects.create(name=’Alen’)

....

authors=Author.objects.all()

b1=Book()

b1.name=’python book1’

b1.save()

alen=Author.objects.get(name__exact=’Alen’)

b1.authors.add(alen)  #add remove filter(name__exact=’Alen’)...

b1.authors.add(authors[1])

....

b1.authors.all()

...

alen.book_set.all() alen.book_set.add(b1)  

alen.book_set.create(name=’python book2’)  alen.book_set.remove() 

 

5、表单方法

表单方法很有用,可以让javascript中操作得到的数据利用表单上传到服务器,进而操作数据库

利用url默认为get方法,submit提交后为POST方法

点击submit后会触发post方法。(req.POST参数能在用户输入为空的的时候反馈错误信息)

在稍后的文章中会专门细讲利用表单成功操作数据库(mongodb)的案例。

 

 


0 0
原创粉丝点击