首先来看视图(view),在用Django生成的网站文件夹中,创建一个view.py文件,这个文件开始是空的。然后我们输入以下内容:
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")
在我们使用django生成的网站中可以找到一个urls.py的文件,我们打开这个文件,按照文件中提示的例子,加入我们想要加入的路径以及对应的视图。
整个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() urlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘Bidding.views.home‘, name=‘home‘), # url(r‘^Bidding/‘, include(‘Bidding.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‘^hello/$‘, ‘Bidding.views.hello‘), )
import datetime def current_datetime(request): now = datetime.datetime.now() html = "<html><body>现在时间是 %s.</body></html>" % now return HttpResponse(html)再新增加的这个视图中,我们使用了一个变量now,用来输出现在的时间。然后我们需要修改一下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() urlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘Bidding.views.home‘, name=‘home‘), # url(r‘^Bidding/‘, include(‘Bidding.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‘^hello/$‘, ‘Bidding.views.hello‘), url(r‘^time/$‘, ‘Bidding.views.current_datetime‘), )
# -*- coding: utf-8 -*-
在接下来,我们来实现一下动态的url。创建第三个视图来显示当前时间和加上时间偏差量的时间,设计是这样的: /time/plus/1/ 显示当前时间+1个小时的页面 /time/plus/2/ 显示当前时间+2个小时的页面 /time/plus/3/ 显示当前时间+3个小时的页面,以此类推。
在urls.py文件中添加(r‘^time/plus/\d{1,2}/$‘, hours_ahead),其含义一定能够看明白了吧 ,如果你对正则不熟悉,那要先熟悉一下了,这里不多过解释正则式了。
这里面需要注意的是我们需要把用户输入的url中提取一个数作为参数传递给视图。根据上面的正则知道,我们要提取的就是加的小时,也就是“\d{1,2}”这部分(0~99),然而正则中选取部分也是用()这里,需要我们把“\d{1,2}”用括号括起来(r‘^time/plus/(\d{1,2})/$‘, hours_ahead)
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘Bidding.views.home‘, name=‘home‘), # url(r‘^Bidding/‘, include(‘Bidding.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‘^hello/$‘, ‘Bidding.views.hello‘), url(r‘^time/$‘, ‘Bidding.views.current_datetime‘), url(r‘^time/plus/(\d{1,2})/$‘, ‘Bidding.views.hours_ahead‘), )现在开始写 hours_ahead 视图。
def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>再过 %s 个小时, 时间将会是 %s.</body></html>" % (offset, dt) return HttpResponse(html)上面的代码不难理解,这里就不多说了。至于上面的try和except就是为了隐藏错误信息的,作为程序员的你一定懂得 。
“2014年CityEngine三维建模与设计精英培训班”——全国巡回举办,布布扣,bubuko.com
“2014年CityEngine三维建模与设计精英培训班”——全国巡回举办
原文:http://blog.csdn.net/arcgis_all/article/details/24264621