views.py代码
from django.http import HttpResponse
import time
import time
def index(request):
return HttpResponse("hello world")
from django.http import HttpResponse
def today(request):
now=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
now_string="today is %s"%now
return HttpResponse(now_string)
def sayhello(request,name):
result="hello 天主极乐大帝 是%s"%name
return HttpResponse(result)
def birthday(request,year,month,day):
if int(year)>2019:
year=str(2019)
if int(month)>12:
month=str(12)
if int(day)>31:
day=str(31)
date="%s-%s-%s 00:00:01"%(year,month,day)
date0="%s-01-01 00:00:00"%(year)
date1=time.strptime(date,'%Y-%m-%d %H:%M:%S')
date2 = time.strptime(date0, '%Y-%m-%d %H:%M:%S')
timestamp1=int(time.mktime( date1))
timestamp2 = int(time.mktime(date2))
dayname=(timestamp1-timestamp2)//86400+1
xianshi='我是%d年第%d天出生的。'%(int(year),dayname)
print(xianshi)
return HttpResponse(xianshi)
from django.contrib import admin
from django.urls import path,re_path
from Blog.views import *
#import多个包用,分割
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index),
#re_path('',index),#b不推荐
re_path('^$',index),#^第一个元素为空,$最后一个元素为空,推荐
path('today/',today),
re_path('sayhello/(\w+)',sayhello),#\w+一个或多个字母(汉字)、数字下划线
re_path('sayhello/(?P<name>\w+)',sayhello),#记住写逗号
#当路由当中有命名组匹配,租的名称和视图的参数名称必须要对应
re_path('birthday/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})',birthday),
]
运行结果截图