1、django安装
python 2.6 要使用 Django-1.4.5.tar.gz
[root@localhost yum.repos.d]# pip install Django==1.4.5 Downloading/unpacking Django==1.4.5 Downloading Django-1.4.5.tar.gz (7.7MB): 7.7MB downloaded Running setup.py (path:/tmp/pip_build_root/Django/setup.py) egg_info for package Django Installing collected packages: Django Running setup.py install for Django changing mode of build/scripts-2.6/django-admin.py from 644 to 755 changing mode of /usr/bin/django-admin.py to 755 Successfully installed Django Cleaning up... [root@localhost ~]# python Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>>
2、django 使用,第一个project
[root@localhost pyfile]# django-admin.py startproject cvst01 [root@localhost cvst01]# pwd /root/pyfile/cvst01 [root@localhost cvst01]# ls cvst01 manage.py
vi settings.py TIME_ZONE = 'Asia/Shanghai' # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'zh-cn' INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', )
3、第一个web app应用 [root@localhost cvst01]# django-admin.py startapp blog [root@localhost cvst01]# pwd /root/pyfile/cvst01 [root@localhost cvst01]# ls blog cvst01 manage.py [root@localhost cvst01]# cd blog [root@localhost blog]# ls __init__.py models.py tests.py views.py vi urls.py ## 映射一个动作,当访问blog/index的时候,就调用blog应用里面,views模块里的index方法 urlpatterns = patterns('', # Examples: # url(r'^$', 'cvst01.views.home', name='home'), # url(r'^cvst01/', include('cvst01.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^blog/index/$','blog.views.index'), )
##使用index方法,返回一个HttpResponse [root@localhost blog]# pwd /root/pyfile/cvst01/blog vi view.py from django.http import HttpResponse def index(req): return HttpResponse('hello welcome to Django
')
[root@localhost cvst01]# ls blog cvst01 manage.py [root@localhost cvst01]# python manage.py runserver Validating models... 0 errors found Django version 1.4.5, using settings 'cvst01.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [07/Apr/2016 22:08:57] "GET / HTTP/1.1" 404 1900 [07/Apr/2016 22:09:10] "GET /blog/index HTTP/1.1" 301 0 [07/Apr/2016 22:09:10] "GET /blog/index/ HTTP/1.1" 200 32 [07/Apr/2016 22:09:15] "GET /blog/ HTTP/1.1" 404 1915 [07/Apr/2016 22:09:20] "GET /blog/index/ HTTP/1.1" 200 32
4、上面的“
HttpResponse('hello welcome to Django
')
”使用文本,现在学习使用html,html文件都是放在app目录的templates下,新建这个目录,在里面加入一个html文件
vi index.html
CVST
hello everyone!
那么映射响应的动作也变了,所以修改views.py,有两种方法,分别导入不同的模块
[root@localhost blog]# cat views.py 第一种 from django.http import HttpResponse from django.template import loader,Context def index(req): t = loader.get_template('index.html') c = Context({}) return HttpResponse(t.render(c)) 第二种 from django.shortcuts import render_to_response def index(req): return render_to_response('index.html',{})
5、
如何在视图中,动态传入数据
使用模板变量,用{{}}表示
`title`
hello `user`.`name`!
age:`user`.`age`
sex:`user`.`sex`
the `user`.`name` say : `user`.`say`
age:`book_list`.`0`
age:`book_list`.`1`
class Person(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def say(self): return 'I am ' + self.name #可以接收变量(my page),字典 user{}等,也可以接收对象user def index(req): # user = {'name':'kate','age':'23','sex':'male'} book_list = ['Python','Java','Shell'] user = Person('Tom',23,'male') return render_to_response('index.html',{'title':'my page','user':user,'book_list':book_list})