编译环境采用的是PyCharm
新建Django项目
如图:其中views.py是自己创建的
会创建一个test2 venv manage.py文件
venv是一个依赖性文件
创建一个new python file
views.py:
from django.http import HttpResponseimport timedef current_time(request): return HttpResponse("Current time is:"+time.strftime('%Y-%m-%d %H:%M:%S'))首先从django.http模块引入HttpResponse类然后定义了一个叫current_time 的视图,每个视图函数至少有一个参数,通常被叫做request这个函数只有简单的一行代码: 它仅仅返回一个HttpResponse对象。进行url配置
在urls.py中配置:urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^current_time/$', current_time),]
运行结果:http://127.0.0.1:8000/current_time/
转载于:https://www.cnblogs.com/1998lu/p/9123045.html