Django学习笔记(一)
2014-03-11 16:21
274 查看
一、Django介绍
Django是一个开放源代码的Web应用框架,由python编写。采用了MVC的软件设计模式,即模型M,视图V和控制器C。它最初是被开发来用于管理劳伦斯出版集团放下的一些以新闻内容为主的网站的,即CMS(内容管理系统)软件。并于2005年7月在BSD许可证下发布。Django框架的核心组件有:
1.用于创建模型的对象关系映射
2.为最终天启设计的完美管理界面
3.一流量的URL设计
4.设计者友好的模版语言
5.缓存系统
二Django工作机制:
1.用manage.py runserver 启动Django服务器时就载入了在同一目录下的settings .py。该文件包含了项目中的配置信息,如前面讲的URLConf等,其中最重要的配置就是ROOT_URLCONF,它告诉Django哪个Python模块应该用作本站的URLConf,默认的是urls .py2.当访问url的时候,Django会根据ROOT_URLCONF的设置来装载URLConf。3.然后按顺序逐个匹配URLConf里的URLpatterns。如果找到则会调用相关联的视图函数,并把HttpRequest对象作为第一个参数(通常是request)4.最后该view函数负责返回一个HttpResponse对象。三、Django下载安装
Django可以运行在Apache上,也可以运行在支持WSGI,FastCGI的服务器上。支持多种数据库,已经支持Postgresql,MySql, Sqlite3,Oracle。Google App Engine也支持Django的某些部分,国内支持的平台有(SAE)Sina App Engine,(BAE)百度应用引擎。安装很简单,如下:
wget https://www.djangoproject.com/download/1.6.2/tarball/ tar xzvf Django-1.6.2.tar.gz cd Django-1.6.2 python setup.py install
四、使用Django
1、启动项目
[root@CnDkItvssh2 bamboo]#django-admin.py startproject testproject[root@CnDkItvssh2 bamboo]# tree
.
└── testproject
├── manage.py
└──testproject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 5 files
2、创建应用程序
[root@CnDkItvssh2 bamboo]# cd testproject/[root@CnDkItvssh2 testproject]# ls
manage.py testproject
[root@CnDkItvssh2 testproject]# pythonmanage.py startapp testapp
[root@CnDkItvssh2 testproject]# cd testapp/
[root@CnDkItvssh2 testapp]# tree
.
├── admin.py
├── __init__.py
├── models.py
├── tests.py
└── views.py
0 directories, 5 files
为了使 Django 认识到新应用程序的存在,还需要向 settings.py 文件中的 INSTALLED_APPS 添加一个条目。对于这个应用程序来说,必须添加字符串testapp
[root@CnDkItvssh2 testproject]# vimtestproject/settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testapp', )
3、创建视图
[root@CnDkItvssh2 testproject]# vimtestapp/views.py from django.shortcuts import render from django.http import HttpResponse def index(req): return HttpResponse('<h1>Hello world!</h1>')
4、配置urls.py
[root@CnDkItvssh2 testproject]# vimtestproject/urls.pyurl(r'^testapp/index/$','testapp.views.index')
5、启动Django
[root@CnDkItvssh2testproject]# python manage.py runserver 0.0.0.0:8002Validating models...
0 errors found
March 11, 2014 - 07:18:50
Django version 1.6.2, using settings'testproject.settings'
Starting development server athttp://0.0.0.0:8002/
Quit the server with CONTROL-C.
[11/Mar/2014 07:18:58] "GET/testapp/index/ HTTP/1.1" 200 21
6、测试访问
访问如下URL: http://192.168.6.28:8002/testapp/index/本文出自 “bambooes” 博客,请务必保留此出处http://bambooes.blog.51cto.com/102565/1372683
相关文章推荐
- Andriod 打包 签名,发布到Google play
- golang 使用template funcmap的规则
- The Django template language 阅读批注
- uva 11054 Wine trading in Gergovia
- uva 11054 Wine trading in Gergovia
- Chrome(Google浏览器)常用插件
- google查询有感
- 12 Benefits of Golang For Enterprise Systems
- hdu 4362 Dragon Ball(dp)
- HDOJ 3482 Good Serial Inc.
- django用表单实现图片爬虫
- go语言限制Goroutine数量
- 选择Go语言的12个理由
- OGG_GoldenGate复杂参数定义(案例)
- 6182 Ginkgo Numbers
- Django的安装
- GODADDY 优质DNS 未被墙
- hdu3635 Dragon Balls
- Got a packet bigger than 'max_allowed_packet' byte
- django 一次性上传多个文件, 批量上传