1. Django Installation
Python 2.6 to use 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", "CR"Edits " or " license " for more information.>>> import django>> >
2, Django use, the first 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. The first Web app application [[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## map an action, when accessing Blog/index, call the blog application inside, the View module index method 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方法, returns a httpresponse[[email protected] blog]# pwd/ Root/pyfile/cvst01/blogvi view.pyfrom django.http import httpresponsedef index (req): Return httpresponse ('
[[email protected] cvst01]# lsblog cvst01 manage.py[[email protected] cvst01]# python manage.py runservervalidating mode Ls... 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/h ttp/1.1 "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, the above "
HttpResponse ('
"Using text, now learning to use the html,html file is placed in the app directory under the templates, create this directory, add an HTML file inside
VI index.html
So the mapping response has changed, so modify the views.py, there are two ways to import different modules separately
[email protected] blog]# cat views.py The first from django.http import httpresponsefrom django.template Import loader, Contextdef Index (req): t = loader.get_template (' index.html ') c = Context ({}) return HttpResponse (t.re NDEr (c)) the second from Django.shortcuts Import Render_to_responsedef index (req): Return render_to_response (' index.html ', {} )
5.
How to dynamically pass in data in a view
Using template variables, denoted by {{}}
class Person (object):d EF __init__ (self,name,age,sex): Self.name = Nameself.age = Ageself.sex = Sexdef Say (self): return ' I Am ' + self.name# can receive variables (my page), dictionary user{}, etc., and can also receive objects Userdef index (req): #user = {' name ': ' Kate ', ' age ': ' + ', ' sex ': ' Male '} Book_list = [' Python ', ' Java ', ' Shell ']user = person (' Tom ', ' Male ') return Render_to_response (' index.html ', {' title ') : ' My page ', ' user ': User, ' Book_list ': book_list})
"Python Learning" Django installation, the first app