目录
一、web2py
二、使用步骤
1.找到一个可以帮助网站运营的工具——pythonanywhere
1)创建账户;
2)创建网站
3) 网站管理
2.我的开端
1)删除example app
2)创建新的app sophia
3)修改参数
4)修饰
前言
已经想学开发网站很久了,奈何只会Python,但不会HTML语言。
一、web2py
当初学时觉得最容易上手的工具。
二、使用步骤
1.找到一个可以帮助网站运营的工具——pythonanywhere
入口:https://www.pythonanywhere.com/
1)创建账户;
Start running……;
Create a Beginner account;
注册;
创建成功;
2)创建网站
点击Open Web tab后Add a new web app(我已经有一个创建好的网站被我停用了,这时免费的网站只能创建一个,所以需要删除原来的网站才能创建新的);
一路next(什么注册选择都自己选);
using this domain already exists说明网络仓库已经有这个文件夹了,而创建新的网站APP需要空的文件路径,因此直接把上面的Directory路径修改一下就好了,Admin Password是网站管理密码(创建需要等待几分钟);
成功创建,接下来是网站管理了;
每当你对网站进行修改后,若是刷新网站后没有更新,则是要点击下面的绿色按钮Reload ……;
若是你想一直让这个网站运行下去,则是要每隔三个月点击黄色按钮Run……;
3) 网站管理
以我的注册网站为例,进入welcome
这是网站的后台入口界面,点击蓝色按钮admin;
输入管理密码;
到达后台;
这些东西到这都可以自己探索了,可以自己随便试试。
2.我的开端
1)删除example app
2)创建新的app sophia
3)修改参数
对menu.py
主要是看上面的菜单栏有些不爽,将他们都隐去。
记得保存。
# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations# ----------------------------------------------------------------------------------------------------------------------
# this is the main application menu add/remove items as required
# ----------------------------------------------------------------------------------------------------------------------response.menu = [(T('sophia'), False, URL('default', 'index'), [])
]# ----------------------------------------------------------------------------------------------------------------------
# provide shortcuts for development. you can remove everything below in production
# ----------------------------------------------------------------------------------------------------------------------
'''
if not configuration.get('app.production'):_app = request.applicationresponse.menu += [(T('My Sites'), False, URL('admin', 'default', 'site')),(T('This App'), False, '#', [(T('Design'), False, URL('admin', 'default', 'design/%s' % _app)),(T('Controller'), False,URL('admin', 'default', 'edit/%s/controllers/%s.py' % (_app, request.controller))),(T('View'), False,URL('admin', 'default', 'edit/%s/views/%s' % (_app, response.view))),(T('DB Model'), False,URL('admin', 'default', 'edit/%s/models/db.py' % _app)),(T('Menu Model'), False,URL('admin', 'default', 'edit/%s/models/menu.py' % _app)),(T('Config.ini'), False,URL('admin', 'default', 'edit/%s/private/appconfig.ini' % _app)),(T('Layout'), False,URL('admin', 'default', 'edit/%s/views/layout.html' % _app)),(T('Stylesheet'), False,URL('admin', 'default', 'edit/%s/static/css/web2py-bootstrap3.css' % _app)),(T('Database'), False, URL(_app, 'appadmin', 'index')),(T('Errors'), False, URL('admin', 'default', 'errors/' + _app)),(T('About'), False, URL('admin', 'default', 'about/' + _app)),]),('web2py.com', False, '#', [(T('Download'), False,'http://www.web2py.com/examples/default/download'),(T('Support'), False,'http://www.web2py.com/examples/default/support'),(T('Demo'), False, 'http://web2py.com/demo_admin'),(T('Quick Examples'), False,'http://web2py.com/examples/default/examples'),(T('FAQ'), False, 'http://web2py.com/AlterEgo'),(T('Videos'), False,'http://www.web2py.com/examples/default/videos/'),(T('Free Applications'),False, 'http://web2py.com/appliances'),(T('Plugins'), False, 'http://web2py.com/plugins'),(T('Recipes'), False, 'http://web2pyslices.com/'),]),(T('Documentation'), False, '#', [(T('Online book'), False, 'http://www.web2py.com/book'),(T('Preface'), False,'http://www.web2py.com/book/default/chapter/00'),(T('Introduction'), False,'http://www.web2py.com/book/default/chapter/01'),(T('Python'), False,'http://www.web2py.com/book/default/chapter/02'),(T('Overview'), False,'http://www.web2py.com/book/default/chapter/03'),(T('The Core'), False,'http://www.web2py.com/book/default/chapter/04'),(T('The Views'), False,'http://www.web2py.com/book/default/chapter/05'),(T('Database'), False,'http://www.web2py.com/book/default/chapter/06'),(T('Forms and Validators'), False,'http://www.web2py.com/book/default/chapter/07'),(T('Email and SMS'), False,'http://www.web2py.com/book/default/chapter/08'),(T('Access Control'), False,'http://www.web2py.com/book/default/chapter/09'),(T('Services'), False,'http://www.web2py.com/book/default/chapter/10'),(T('Ajax Recipes'), False,'http://www.web2py.com/book/default/chapter/11'),(T('Components and Plugins'), False,'http://www.web2py.com/book/default/chapter/12'),(T('Deployment Recipes'), False,'http://www.web2py.com/book/default/chapter/13'),(T('Other Recipes'), False,'http://www.web2py.com/book/default/chapter/14'),(T('Helping web2py'), False,'http://www.web2py.com/book/default/chapter/15'),(T("Buy web2py's book"), False,'http://stores.lulu.com/web2py'),]),(T('Community'), False, None, [(T('Groups'), False,'http://www.web2py.com/examples/default/usergroups'),(T('Twitter'), False, 'http://twitter.com/web2py'),(T('Live Chat'), False,'http://webchat.freenode.net/?channels=web2py'),]),]
'''
修改default.py
由于需要自定义网站,所以需要将默认网站接口进行修改。将代码修改以下:
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------
# This is a sample controller
# this file is released under public domain and you can use without limitations
# -------------------------------------------------------------------------# ---- example index page ----
def index():return auth.wiki() # ---- API (example) -----
@auth.requires_login()
def api_get_user_email():if not request.env.request_method == 'GET': raise HTTP(403)return response.json({'status':'success', 'email':auth.user.email})# ---- Smart Grid (example) -----
@auth.requires_membership('admin') # can only be accessed by members of admin groupd
def grid():response.view = 'generic.html' # use a generic viewtablename = request.args(0)if not tablename in db.tables: raise HTTP(403)grid = SQLFORM.smartgrid(db[tablename], args=[tablename], deletable=False, editable=False)return dict(grid=grid)# ---- Embedded wiki (example) ----
def wiki():auth.wikimenu() # add the wiki to the menureturn auth.wiki() # ---- Action for login/register/etc (required for auth) -----
def user():"""exposes:http://..../[app]/default/user/loginhttp://..../[app]/default/user/logouthttp://..../[app]/default/user/registerhttp://..../[app]/default/user/profilehttp://..../[app]/default/user/retrieve_passwordhttp://..../[app]/default/user/change_passwordhttp://..../[app]/default/user/bulk_registeruse @auth.requires_login()@auth.requires_membership('group name')@auth.requires_permission('read','table name',record_id)to decorate functions that need access controlalso notice there is http://..../[app]/appadmin/manage/auth to allow administrator to manage users"""return dict(form=auth())# ---- action to server uploaded static content (required) ---
@cache.action()
def download():"""allows downloading of uploaded fileshttp://..../[app]/default/download/[filename]"""return response.download(request, db)
视图修改layout.html
修改掉web2py这个logo;
找到这一行:
改成:
修改index.py;
将这些代码全换成我下面的这个:
{{extend 'layout.html'}}{{=content}}
到这基本参数修改完毕。来看看最终呈现的网站是什么样子的。
嗯,网站干净整洁,达到预期。
4)修饰
注册;
在这个网站进行二次注册,注意密码不能太短也不能太长,要不然注册不了。(名字啊,随便取)
多一个 Edit Page等菜单栏,这下可以对这个网站肆意妄为了。
Edit Page页(可以看看我的编辑)
Edit Page Media
提交
最终结果呈现
总结
没什么好总结的,一开始弄这个也是为了一有点喜欢的人,单箭头的喜欢,嗯,先优秀自己。