python下的web开发框架 Django,url配置
url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
修改 urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^newtest/', include('newtest.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^$', 'newtest.helloworld.index'),
# Uncomment the next line to enable the admin:
# (r'^admin/(.*)', admin.site.root),
)
其中
(r'^$', 'polls.helloworld.index'),
r’^$’ 是为了匹配空串 ,就是http://localhost:8000/
r’^$’, ‘polls.helloworld.index’),就是说 http://localhost:8000/ 这个地址将会指向
polls 这个工程里 helloworld.py 文件里定义的index方法,看看helloworld.py里面是不是有个def index(request):
举个例子,如果以后要想让http://localhost:8000/ blog 能被访问,只需要加个
r’^blog’ ,然后在后面写views来控制结果(这个以后会讲),够方便吧!
如果此时 web server已经启动,直接刷新页面就可以看到 hello world 了
很方便,django的url配置helloworld就是这样
相关文档:
Mako是什么?Moko是Python写的一个模板库,Python官网python.org用的就是它哦。其他废话也就不累赘了,直接来点代码,方便阅读与了解把。
(Mako官网地址:http://www.makotemplates.org/ ,可以下载安装包,推荐使用easy_install安装)
from mako.template import Template
mytemplate = Template("hello world!") ......
Ubuntu平台下的Python操作Mysql
1.安装Ubuntu,安装Msql.
2.打开终端,输入 python
import MySQLdb
con = MySQLdb.connect(db="python")
cur = con.cursor()
count = cur.execute("select * from test")
print count
data = cur.fetchall()
print data
for d in data:
print d
import os
os.system('clear') ......
以前没有写过python脚本,于是找了一个简易的教程过了一遍于是就是干了。
这两天测试mysql archive引擎的性能,于是用python向archive表中插入10亿条数据,python大致是如下写的:
for i in range(0,100000000)
insert into ....
结果执行之后系统就死机了,求助“伟哥”,最后发现再执行脚本的时候,for in ......
Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
>>> os. ......