from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")
from django.conf.urls import url from django.contrib import admin from mysite.views import hello urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^hello/$', hello), ]
在浏览器訪问http://127.0.0.1:8000/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 import url from django.contrib import admin from mysite.views import hello , current_datetime urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^hello/$', hello), url(r'^time/$', current_datetime), ]
from django.http import HttpResponse , Http404 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 hours(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html)
from django.conf.urls import url from django.contrib import admin from mysite.views import hello , current_datetime , hours_ahead urlpatterns = [ url(r'^admin/', admin.site.urls), ... ... url(r'^time/plus/(\d{1,2})/$', hours_ahead), ]