首页 > Web开发 > 详细

视图和URL配置

时间:2015-03-28 22:59:18      阅读:305      评论:0      收藏:0      [点我收藏+]
from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")
from django.conf.urls.defaults import *
from views import hello

urlpatterns = patterns(‘‘,
    (^hello/$, hello),
)

 

from django.http import HttpResponse
import datetime

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)
from django.conf.urls.defaults import *
from views import hello, current_datetime

urlpatterns = patterns(‘‘,
    (^hello/$, hello),
    (^time/$, current_datetime),
)
urlpatterns = patterns(‘‘,
    (^hello/$, hello),
    (^time/$, current_datetime),
    (^another-time-page/$, current_datetime),
)

 

urlpatterns = patterns(‘‘,
    # ...
    (r^time/plus/\d+/$, hours_ahead),
    # ...
  (r‘^time/plus/\d{1,2}/$‘, hours_ahead),
)
from django.conf.urls.defaults import *
from views import hello, current_datetime, hours_ahead

urlpatterns = patterns(‘‘,
    (r^hello/$, hello),
    (r^time/$, current_datetime),
    (r^time/plus/(\d{1,2})/$, hours_ahead),
)
from django.http import Http404, HttpResponse
import datetime

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

 

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    assert False
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

 

视图和URL配置

原文:http://www.cnblogs.com/fish7/p/4374867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!