视图和URL配置

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)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。