法1:在文件中开头加入如下:#!/usr/bin/python 或 者 #!user/bin/env python
#encoding=utf-8
# -*- coding: utf-8 -*- # 推荐这种方式:可以解决模块间导入中文的问题,建议每次都写上[注意的是声明的编码必须与文件实际保存时用的编码一致,否则很大几率会出现代码解析异常。现在的IDE一般会自动处理这种情况,改变声明后同时换成声明的编码保存,但文本编辑器控们需要小心]
#!/usr/bin/python
#encoding=utf-8
# Filename: read_file.py
# 读取数据
import os
data_path = "/home/johnnie/文档/Workspace/python/data/sketch.txt"
if os.path.exists(data_path):
data = open(data_path)
while True:
for line in data.readlines():
if len(line) == 0:
break
else:
# 增加一逻辑判断: 查看当前数据行是否包含 ":" 字符,若包含,则分割数据
# find(): 找出一个字符串中的子串,若没找到,则返回 -1
# not: 对表达式取反
if not line.find(":") == -1:
try:
(role, msg) = line.split(": ", 1)
print ("{0} said:{1}").format(role, msg)
except ValueError:
pass
data.close()
else: # IOError
print "The data file is missing!"
urlpatterns = [
......,
url(r'^sblog/blog_details/(\d+)', 'sblogs.views.blog_detail'),
]
....
....
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.home', name="home"),
# 以递归的方式来配置 url: sblog 下配置了一个 urls.py
url(r'^sblog/', include('sblog.urls')),
]
2) 在 app 下新建 urls.py:
urlpatterns = patterns(('sblog.views'),
url(r'^blog_list', "blog_list", name="blog_list"),
url(r'^blog_detail/(\d+)', 'blog_detail', name="blog_detail"),
)
3) 修改 blog_detail.html 文件: