环境:python 2.7.3 + django 1.4
IDE :eclipse + pydev
下载地址:http://pydev.org/download.html
使用pydev新建一个基于django的sample工程。
本节只要尝试给工程添加view层以及templete代码:
1. 在sample目录下添加views.py文件,代码如下:
from django.http import HttpResponse
import datetime
from django.template import Template, Context
def hello(request):
return HttpResponse("Hello world")
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
def template(request):
t = Template('Hello, {{ name }}')
html = t.render(Context({'name': 'John'}))
return HttpResponse(html)
2. 修改路由文件(urls.py):
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from sample.views import hello
from sample.views import current_datetime
from sample.views import template
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.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)),
('^hello/$', hello),
('^time/$', current_datetime),
('^template/$', template),
)
运行程序,访问 http://localhost:8000/hello等即可。
接下来介绍如何使用html模版。
1. sample目录下新建templates文件夹用来放置html模板文件。
2. 修改settings.py文件中TEMPLATE_DIRS的配置,例如:
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)
3. 在templates文件夹下新建测试用的html模板文件,例如:hello.html
Hello, {{ name }}
4. 在views.py文件中追加如下代码:
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
def say_hello(request):
t = get_template('hello.html')
html = t.render(Context({'name': 'Shen Bin'}))
return HttpResponse(html)
5. 追加路由:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from sample.views import hello
from sample.views import current_datetime
from sample.views import template
from sample.views import say_hello
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.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)),
('^hello/$', hello),
('^time/$', current_datetime),
('^template/$', template),
('^say_hello/$', say_hello),
)
重启服务后即可访问: http://localhost:8000/say_hello
最后,为了简化代码,我们可以使用render_to_response方法。
views.py增加一个新的方法:
from django.shortcuts import render_to_response
def say_hello2(request):
return render_to_response('hello.html', {'name': 'Chen Yan'})
在urls.py里增加一个路由:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from sample.views import hello
from sample.views import current_datetime
from sample.views import template
from sample.views import say_hello
from sample.views import say_hello2
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.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)),
('^hello/$', hello),
('^time/$', current_datetime),
('^template/$', template),
('^say_hello/$', say_hello),
('^say_hello2/$', say_hello2),
)