码迷,mamicode.com
首页 > Web开发 > 详细

视图和URL配置

时间:2015-03-28 23:00:02      阅读:179      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!