mdwiki开发之路二资源与踩坑记录

一、bootstrap代码片断

若是你没有艺术细胞,偷懒的方法就是到这上面去找,好比登陆框界面等。
侧边栏选用:http://www.designerslib.com/b...提到的http://bootsnipp.com/fullscre...
其余一些资源:
w3schools-howto
一个比较炫的html模板(虽然最后没有采用)
bootstrap主题javascript

二、DIV的CSS height:100%无效的解决办法:

在css当中增长上:css

html, body{ margin:0; height:100%; }

三、Alembic migration失败,Sqlite lack of ALTER support解决办法:

在env.py中设置render_as_batch=Truehtml

context.configure(
    connection=connection,
    target_metadata=target_metadata,
    render_as_batch=True
)

四、markdown扩展:

http://pythonhosted.org/Markd...
比较有用的
Table of Contents(toc)、
CodeHilite(代码高亮)、
Meta-Data(文件前面能够添加元数据,好比标题,做者等)、
New Line to Break(换行即新行,而不是像原生markdown那样得换两行)、
Tables(表格插件)java

五、关于Flask的:

Flask request,g,session的实现原理
深刻 Flask 源码理解 Context
Flask Session超时设置
默认状况下,flask session在你关闭浏览器的时候失效。你能够经过设置permanent session来改变这一行为。python

from datetime import timedelta
from flask import session, app

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=30)

默认状况下,permanent_session_lifetime是31天。nginx

六、关于SQLAlchemy:

SQLAlchemy 使用经验
SqlAlchemy query many-to-many relationshipgit

class Restaurant(db.Model):
    ...

    dishes = db.relationship('Dish', secondary=restaurant_dish,
        backref=db.backref('restaurants'))

而后检索全部的dishes for a restaurant, you can do:github

x = Dish.query.filter(Dish.restaurants.any(name=name)).all()

产生相似以下SQL语句:sql

SELECT dish.*
FROM dish
WHERE
    EXISTS (
        SELECT 1
        FROM restaurant_dish
        WHERE
            dish.id = restaurant_dish.dish_id
            AND EXISTS (
                SELECT 1
                FROM restaurant
                WHERE
                    restaurant_dish.restaurant_id = restaurant.id
                    AND restaurant.name = :name
            )
    )

七、解决循环import的问题思路

1.延迟导入(lazy import)
即把import语句写在方法或函数里面,将它的做用域限制在局部。
这种方法的缺点就是会有性能问题。
2.将from xxx import yyy改为import xxx;xxx.yyy来访问的形式
3.组织代码
出现循环import的问题每每意味着代码的布局有问题,能够合并或者分离竞争资源。合并的话就是都写到一个文件里面去。分离的话就是把须要import的资源提取到一个第三方文件去。总之就是 将循环变成单向。
具体解决方案后续文章再贴代码django

八、关于Python的一些:

Good logging practice in Python
How do I check if a variable exists?
To check the existence of a local variable:

if 'myVar' in locals():
  # myVar exists.

To check the existence of a global variable:

if 'myVar' in globals():
  # myVar exists.

To check if an object has an attribute:

if hasattr(obj, 'attr_name'):
  # obj.attr_name exists.
if('attr_name' in dir(obj)):
    pass

还有一个不是很优雅地方案,经过捕获异常的方式:

try:
    myVar
except NameError:
    myVar = None
# Now you're free to use myVar without Python complaining.

九、关于Git与Github

How do I delete a Git branch with TortoiseGit
为何给GIT库打TAG不成功

如何修改github上仓库的项目语言?

项目放在github,是否是常常被识别为javascript项目?知乎这篇问答给出了答案。
问题缘由:
github 是根据项目里文件数目最多的文件类型,识别项目类型.
解决办法:
项目根目录添加 .gitattributes 文件, 内容以下 :

*.js linguist-language=python

做用: 把项目里的 .js 文件, 识别成 python 语言.

十、关于IDE的:

Indexing excluded directories in PyCharm
pycharm convert tabs to spaces automatically

十一、关于Celery的:

periodic task for celery sent but not executed
这个因为我没仔细看官方文档,搞了很久。Celery的周期性任务scheduler须要配置beat和运行beat进程,可是仅仅运行beat进程能够吗?不行!我就是这里被坑了。还得同时运行一个worker。也就是说beat和worker都须要经过命令行运行。对于周期性任务beat缺一不可。其余任务可仅运行worker。

十二、在supervisor或gunicorn设置环境变量

若是采用gunicorn命令行的形式:-e选项

gunicorn -w 4 -b 127.0.0.1:4000 -k gevent -e aliyun_api_key=value,SECRET_KEY=mysecretkey app:app

若是采用gunicorn.conf.py文件的形式:raw_env

import multiprocessing

bind = "127.0.0.1:4000"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class='gevent'
proc_name = "mdwiki"
user = "nginx"
chdir='/opt/mdwiki'
#daemon=False
#group = "nginx"
loglevel = "info"
#errorlog = "/home/myproject/log/gunicorn.log"
#accesslog=
raw_env = [
   'aliyun_api_key=value',
   'aliyun_secret_key=value',
   'MAIL_PASSWORD=value',
   'SECRET_KEY=mysecretkey',
]
#ssl
#keyfile=
#certfile=
#ca_certs=

若是采用supervisor配置环境变量

[program:mdwiki]
environment=SECRET_KEY=value,aliyun_api_key=value,aliyun_secret_key=value,MAIL_PASSWORD=value
command=/usr/bin/gunicorn -n mdwiki -w 4 -b 127.0.0.1:4000 -k gevent app:app 
directory=/opt/mdwiki
user=nginx
autostart=true
autorestart=true
redirect_stderr=true
相关文章
相关标签/搜索