Python 3.6+Django开发入门小案例(自动变化的问候)完整步骤
来源:互联网 发布:勤思考研怎么样知乎 编辑:程序博客网 时间:2024/05/21 15:59
第一步:在命令提示符环境使用pip install django命令安装django
第二步:在命令提示符环境使用python django-admin.py startproject django_greeting命令创建网站django_greeting
第三步:利用资源管理器在网站目录django_greeting中创建子文件夹templates,并在该子文件夹中创建文件greeting.html,其中内容如下:
<html>
<body>
<h1>Good {{morning_afternoon_evening}}, I am{{name}}.</h1>
</body>
</html>
第四步:在文件夹django_greeting\django_greeting中创建Python程序文件view.py,其中内容为:
import os
from os.path import join, split, dirname
from random import choice
import datetime
from django.http import HttpResponse, Http404
from django import template
from django.conf import settings
names = ('Zhang san', 'Li si', 'Wang wu', 'Ma liu')
def greeting(request):
templateFile = join(split(dirname(__file__))[0],
'templates')\
+'\\greeting.html'
#打开网页模板文件,创建模板
with open(templateFile) as fp:
t = template.Template(fp.read())
#随机选择一个问候人
current_name = choice(names)
#当前时间
h = datetime.datetime.now().hour
#上午
if 0<=h<12:
mae = 'Morning'
#下午
elif 12<=h<18:
mae = 'Afternoon'
#晚上
else:
mae = 'Evening'
#渲染模板
con = template.Context({'name':current_name,
'morning_afternoon_evening':mae})
html = t.render(con)
return HttpResponse(html)
第五步:修改文件夹django_greeting\django_greeting中的Python程序文件urls.py,将其中的内容修改为:
from django.conf.urls import include, url
from django.contrib import admin
from django_greeting.view import greeting
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^greeting/$', greeting),
]
第六步:打开命令提示符,切换至网站文件夹django_greeting,执行命令python manage.py runserver 127.0.0.1:8000在本机8000端口启动网站,如图:
最后,打开浏览器并访问http://127.0.0.1:8000/greeting/,多次访问会发现,每次问候的人可能会不一样,并且会根据当前时间自动判断是上午、下午或晚上,下午14:26时访问结果如图:
修改计算机时间为晚上21:27,刷新页面,如图:
附:拙作《Python可以这样学》封面
- Python 3.6+Django开发入门小案例(自动变化的问候)完整步骤
- 开发一个简单的JSP标签完整案例的步骤
- Python入门小案例
- python之web开发二:用Python+Django在Eclipse环境下开发web网站【第一个开发的小案例,按照文章可以成功开发web】
- Mac中安装Python+Django+MySQL完整步骤
- Python和Django的详细安装步骤(转)
- Django完整的开发一个博客系统
- Python Web开发:Django入门学习笔记(1)
- Python Web开发:Django入门学习笔记(2)
- 程序必用javascript精选案例999个(来自哥伦比亚大学的亲切问候)
- 【python】Django web应用开发入门
- python web开发——Django入门
- hibernate学习笔记6--Criteria查询方式、完整小练习(开发步骤)
- 开发完整J2EE的八个步骤
- 开发完整J2EE解决方案的八个步骤
- 开发完整J2EE解决方案的八个步骤
- 八个步骤开发完整的J2EE解决方案
- 完整的开发一个ContentProvider步骤
- spring的动态代理机制细节
- dfs 简单穷竭搜索。AOJ 0525
- [莫队] BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- bootStrap布局容器
- [bzoj] 1067: [SCOI2007]降雨量
- Python 3.6+Django开发入门小案例(自动变化的问候)完整步骤
- 为什么敏捷开发备受欢迎?
- 146. LRU Cache
- TI osal蓝牙协议栈回调分析
- 关于listview的adapter的一些思考
- CSS中!important的使用 本篇文章使用最新的IE10以及firefox与chrome测试 !important优先级最高
- Asp防止网页频繁刷新和强制不缓存的方法
- 【正一专栏】爱是一种信仰
- opengl渲染管线 不能再详细了