標籤:collected package running python
1、django安裝
python 2.6 要使用 Django-1.4.5.tar.gz
[[email protected] yum.repos.d]# pip install Django==1.4.5Downloading/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 DjangoInstalling 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 755Successfully installed DjangoCleaning up...[[email protected] ~]# pythonPython 2.6.6 (r266:84292, Sep 4 2013, 07:46:00) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import django>>>
2、django 使用,第一個project
[[email protected] pyfile]# django-admin.py startproject cvst01[[email protected] cvst01]# pwd/root/pyfile/cvst01[[email protected] cvst01]# lscvst01 manage.py
vi settings.pyTIME_ZONE = ‘Asia/Shanghai‘# Language code for this installation. All choices can be found here:# http://www.i18nguy.com/unicode/language-identifiers.htmlLANGUAGE_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應用[[email protected] cvst01]# django-admin.py startapp blog[[email protected] cvst01]# pwd/root/pyfile/cvst01[[email protected] cvst01]# lsblog cvst01 manage.py[[email protected] cvst01]# cd blog[[email protected] blog]# ls__init__.py models.py tests.py views.pyvi 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[[email protected] blog]# pwd/root/pyfile/cvst01/blogvi view.pyfrom django.http import HttpResponsedef index(req):return HttpResponse(‘<h1>hello welcome to Django</h1>‘)
[[email protected] cvst01]# lsblog cvst01 manage.py[[email protected] cvst01]# python manage.py runserverValidating models...0 errors foundDjango 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(‘<h1>hello welcome to Django</h1>‘)
”使用文本,現在學習使用html,html檔案都是放在app目錄的templates下,建立這個目錄,在裡面加入一個html檔案
vi index.html
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>CVST</title></head><body><h1>hello everyone!</h1></body></html>
那麼映射響應的動作也變了,所以修改views.py,有兩種方法,分別匯入不同的模組
[[email protected] blog]# cat views.py第一種from django.http import HttpResponsefrom django.template import loader,Contextdef index(req): t = loader.get_template(‘index.html‘) c = Context({}) return HttpResponse(t.render(c))第二種from django.shortcuts import render_to_responsedef index(req): return render_to_response(‘index.html‘,{})
5、
如何在視圖中,動態傳入資料
使用模板變數,用{{}}表示
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>{{title}}</title></head><body><h1>hello {{user.name}}!</h1><li>age:{{user.age}}</li><li>sex:{{user.sex}}</li><div>the {{user.name}} say : {{user.say}}</div><li>age:{{book_list.0}}</li><li>age:{{book_list.1}}</li></body></html>
class Person(object):def __init__(self,name,age,sex):self.name = nameself.age = ageself.sex = sexdef say(self):return ‘I am ‘ + self.name#可以接收變數(my page),字典 user{}等,也可以接收對象userdef 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})
【python學習】django安裝,第一個應用