Django开发网站(二)
第一课:视图显示
1 建立一个项目:django-admin startproject blog,
进入blog: cd blog
显示:blog(__init__.py settings.py urls.py ) manage.py
2,在当前目录,建立一个应用:django-admin startapp appblog
显示:appblog(__init__.py modules.py views.py tests.py) blog manage.py
3 配置应用vim blog/settings.py下的INSTALLED_APPS (‘appblog',)
编写响应vim blog/urls.py
url(r'^blog/index/$','blog.views.index'),
编写视图vim appblog/views.py:编写def文件,
from django.http import HttpResponse
def index(req):
return HttpResponse('<h1>hello world to Django!</h1>')
4启动项目:python manage.py runserver
第二课:模板映射
1 首先看结构图
1 首先建立外框,即项目:django-admin startproject mysite
然后建立中间框,即应用和模板
django-admin startapp myapp
mkdir templates
2 编写最内框,即各文件
vi templates/index.html
1 <html> 2 <body> 3 <meta http-equiv="Content-Type" content="text/html" /> 4 <title>mytile my index</title> 5 </head> 6 <body> 7 <h1>mybody my index</h1> 8 </body> 9 </html> 10 ~
vi mysite/urls.py
1 from django.conf.urls import patterns, include, url 2 3 # Uncomment the next two lines to enable the admin: 4 # from django.contrib import admin 5 # admin.autodiscover() 6 7 urlpatterns = patterns('', 8 # Examples: 9 # url(r'^$', 'mytest.views.home', name='home'), 10 # url(r'^mytest/', include('mytest.foo.urls')), 11 12 # Uncomment the admin/doc line below to enable admin documentation: 13 # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 14 15 # Uncomment the next line to enable the admin: 16 # url(r'^admin/', include(admin.site.urls)), 17 url(r'^myapp/',include('myapp.urls')), 18 ) 19 ~
vi myapp/urls.py
1 from django.conf.urls import * 2 3 urlpatterns = patterns('', 4 url('^index/$','myapp.views.index'), 5 ) 6 ~ 7 ~ 8 ~
vi myapp/views.py
1 # Create your views here. 2 from django.shortcuts import render_to_response 3 4 def index(req): 5 return render_to_response('index.html') 6 7 ~ 8 ~
vi myapp/settings.py
1 TEMPLATE_DIRS = ( 2 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 3 # Always use forward slashes, even on Windows. 4 # Don't forget to use absolute paths, not relative paths. 5 ####################################### 6 '/home/django/mytest/templates', 7 ) 8 9 INSTALLED_APPS = ( 10 'django.contrib.auth', 11 'django.contrib.contenttypes', 12 'django.contrib.sessions', 13 'django.contrib.sites', 14 'django.contrib.messages', 15 'django.contrib.staticfiles', 16 # Uncomment the next line to enable the admin: 17 # 'django.contrib.admin', 18 # Uncomment the next line to enable admin documentation: 19 # 'django.contrib.admindocs', 20 ############################### 21 'myapp', 22 )
3 启动服务器并在浏览器测试
python manage.py runserver
http://127.0.0.1:8000/myapp/index
4 解析运行原理:
当网页上要请求时,首先进入mysite/urls.py中,执行:
url(r'^myapp/',include('myapp.urls')),
再次调用myapp/urls.py中的文件,执行:
url('^index/$','myapp.views.index'),
接着进入myapp/views视图中的index函数,执行:
def index(req):
return render_to_response('index.html')
这样就返回模板中的index.html文件,即执行templates/index.html
重要的地方:指向 include() 的正则表达式并不包含一个 $ (字符串结尾匹配符),但是包含了一个斜杆。每当 Django 遇到 include() 时,它将截断匹配的URL,并把【剩余】的字符串发往被包含的 URLconf 进一步处理。
django URL常用配置方法