当前位置:编程学习 > 网站相关 >>

Karrigell 入门教程

*Author:Insun

*Blog:http://yxmhero1989.blog.163.com

*From:http://www.4safer.com/forum.php?mod=post&;action=edit&fid=51&tid=507&pid=1707&page=1

*/

本文用的是截止到今日(2011-03-14)最新的Karrigell-3.1.1,2.X版本的路径应该不一样,自己处理。

Karrigell 小推车的意思,真是安心,icon图标也很cute 清晰
karrigell是纯Python web框架,自带web服务器和数据库引擎

0x01. Download & Install

下载:http://sourceforge.net/projects/karrigell/files/Full%20package/

解压运行Karrigell.exe

默认的web主目录是C:Karrigell-3.1.1www

支持 .html  .py  .pih

C:Karrigell-3.1.1www  创建 hello.py,简单的代码 :print "Hello World,Insun is here"

访问:http://localhost/hello.py
2.jpg 

www里面有自带的例子,可以学习。
http://localhost/index.py

1.jpg 

Documentation

No users database





Create it

3.jpg 

4.jpg 
一般选择PyDbLite

5.jpg 
Demos
     --|
Using MySQL Using SQLite Using PyDbLite



6.jpg 
其实都在 C:Karrigell-3.1.1commonadminInstantSite 这个目录里

http://localhost/demo/PyDbLite/blog/index/    blog demo

Others

很好的实例:
http://hyry.dip.jp/index.py
全部使用karrigell构架。

新版本里面是没有Karrigell.ini

参考:http://www.devshed.com/c/a/Python/Karrigell-for-Python/


0x02. HTML forms
www目录下新建test目录,test目录下新建2个文件hello.html和hello.py
hello.html

<form action = "hello.py">
Lets say hello to <input name="user">
<input type="submit" value="OK">
</form>

hello.py
print "Best Hello to " + _user +" From www.4safer.com & Karrigell"

_user也可以写成QUERY[user]
7.jpg 

0x03.Logging and session management
Karrigell使用另一个内置的名字一个叫Session()的函数来管理session
Session对象是web浏览器特有的,因此它能保证和持久一个用户可以被其它脚本标识
可以新建session目录,3个文件
8.jpg 
hello.html 不变
hello.py

print "Hello, " + _user
Session().user = _user
print """What would you like to order :   
<form action="order.py">   
CD <input type="radio" name="type" value="CD">   
book <input type="radio" name="type" value="book">  
phone <input type="radio" name="type" value="phone">   
<input type="submit" value="Ok">   
</form>"""

order.py

print "Hello %s" %Session().user
print "So you wanna order a %s"% _type
9.jpg 

0x04. Authentication
身份认证是一个非常常见的任务用来检查一个用户是否有执行一个任务的权限。因此,要求用户需要输入登录名和密码,程序会在数据库里查看登录名/密码是否合法.
新建目录Authentication
3个文件:login.html Authentication.py order.py
login.html
<form action = "Authentication.py">
Login   <input name="login">
<br>Password<input type="password" name="password">
<input type="submit" value="Ok">


#Authentication.py
def is_valid(login,password):   
# replace this with a check in the users database   
    return login == insun and password == insun
if is_valid(_login,_password):   
    Session().login = _login   
    print "Hello %s <br>" %_login   
    print """What would you like to order :         
         <form action="order.py">        
         CD <input type="radio" name="type" value="CD">        
         <br>book <input type="radio" name="type" value="book">   
         <br>phone <input type="radio" name="type" value="phone">      
         <br><input type="submit" value=" Ok ">        
         </form>"""
else:   
    print "Sorry, you are not allowed to access this page"

#order.py
print "Hello %s" %Session().login
print "So you want to order a %s" %_type

10.jpg 
注意缩进,一

补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,