小编Gab*_*lis的帖子

连接到Flask中的数据库,哪种方法更好?

方法一:使用来自http://flask.pocoo.org/docs/tutorial/dbcon/http://flask.pocoo.org/docs/patterns/sqlite3/的特殊g对象

import sqlite3
from flask import g

DATABASE = '/path/to/database.db'

def connect_db():
    return sqlite3.connect(DATABASE)

@app.before_request
def before_request():
    g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    if hasattr(g, 'db'):
        g.db.close()
Run Code Online (Sandbox Code Playgroud)

方法二:使用来自https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py的 Mysterious _app_ctx_stack

from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash, _app_ctx_stack
def get_db():
    """Opens a new database connection if there is none yet for the
    current application context.
    """
    top = _app_ctx_stack.top
    if not hasattr(top, 'sqlite_db'): …
Run Code Online (Sandbox Code Playgroud)

python sqlite flask

57
推荐指数
4
解决办法
4万
查看次数

Flask URL Route:将多个URL路由到同一个功能

我正在使用Flask 0.9.

现在我想将三个URL路由到同一个函数:

/item/<int:appitemid>
/item/<int:appitemid>/ 
/item/<int:appitemid>/<anything can be here>
Run Code Online (Sandbox Code Playgroud)

<anything can be here>部件永远不会在该功能中使用.

我必须复制相同的功能两次才能实现这个目标:

@app.route('/item/<int:appitemid>/')
def show_item(appitemid):

@app.route('/item/<int:appitemid>/<path:anythingcanbehere>')
def show_item(appitemid, anythingcanbehere):
Run Code Online (Sandbox Code Playgroud)

会有更好的解决方案吗?

python url-routing flask

34
推荐指数
2
解决办法
2万
查看次数

这个" - "在jinja2模板引擎中有什么作用?

我正在学习jinja2,因为Google App Engine推荐它.

我在维基百科上找到了这个例子:http://en.wikipedia.org/wiki/Jinja_%28template_engine%29

  {%- for item in item_list %}
    {{ item }}{% if not loop.last %},{% endif %}
  {%- endfor %}
Run Code Online (Sandbox Code Playgroud)

"{% - for"中的" - "是什么?

另外,我在哪里可以找到jinja2示例(使用Google App Engine更好)?

非常感谢!

python django google-app-engine templates jinja2

20
推荐指数
2
解决办法
7901
查看次数

Flask URL Route:将所有其他URL路由到某个功能

我正在使用Flask 0.9.我有使用Google App Engine的经验.

  1. 在GAE中,url匹配模式按照它们出现的顺序进行评估,先到先得.Flask中的情况是一样的吗?

  2. 在Flask中,如何编写url匹配模式来处理所有其他不匹配的URL.在GAE中,你只需要把它放在/.*最后,比如:('/.*', Not_Found).如果Flask不支持正则表达式,如何在Flask中做同样的事情.

python google-app-engine url-routing flask

13
推荐指数
2
解决办法
1万
查看次数

用随机项替换Python字符串

Python中的字符串替换并不困难,但我想做一些特别的事情:

teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']
#replace 'test' with random item from animals
finalstr = ['dog fox dog monkey']
Run Code Online (Sandbox Code Playgroud)

我写了一个非常低效的版本:

from random import choice
import string
import re

teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']

indexes = [m.start() for m in re.finditer('test', 'test test test test')]
#indexes = [0, 5, 10, 15]
for i in indexes:
    string.replace(teststr, 'test', choice(animals), 1)

#Final result is four random animals
#maybe ['dog fox dog monkey']
Run Code Online (Sandbox Code Playgroud)

它有效,但我相信有一些简单的REGULAR EXPRESSION方法我不熟悉.

python regex string

8
推荐指数
1
解决办法
1890
查看次数

安装curl但libcurl不会更新

我在Ubuntu 12.04上.我刚刚从源代码中将默认CURL从7.22更新到7.28.

wget http://curl.haxx.se/download/curl-7.28.0.tar.gz
./configure
make
make install
Run Code Online (Sandbox Code Playgroud)

但是,libcurl不会更新.当我尝试

curl --version
curl 7.28.0 (i686-pc-linux-gnu) libcurl/7.22.0
Run Code Online (Sandbox Code Playgroud)

如何更新libcurl

ubuntu curl libcurl ubuntu-12.04

8
推荐指数
2
解决办法
8334
查看次数

是否可以通过一个域名使用Google App Engine在Google云端存储上提供静态文件?

首先,www.example.com使用Google App Engine 构建网站https://cloud.google.com/products/

其次,将静态图像上传example.jpg到Google云端存储https://cloud.google.com/products/cloud-storage aka https://developers.google.com/storage/

难道possibe以服务example.jpghttp://www.example.com/images/example.jpg?而不是使用诸如images.example.comcdn.example.com等的子域

我想这样做的原因是因为我希望在必要时让我的网站更容易移动到VPS.

google-app-engine google-cloud-storage

6
推荐指数
1
解决办法
1551
查看次数

使用带有 Unicode 的 Python 正则表达式进行 BackReference

我使用 Python 2.7 使用 Google App Engine,以下是代码片段:

\n\n
# -*- coding: utf-8 -*-\nKEYWORD = u"\xe8\x8b\xb1\xe8\xaa\x9e"\nURL = u"http://www.google.com/"\ncontent = u"\xe5\x92\x8c\xe8\xa3\xbd\xe8\x8b\xb1\xe8\xaa\x9e\xef\xbc\x88\xe3\x82\x8f\xe3\x81\x9b\xe3\x81\x84\xe3\x81\x88\xe3\x81\x84\xe3\x81\x94\xef\xbc\x89\xe3\x81\xa8\xe3\x81\xaf\xe3\x80\x81\xe6\x97\xa5\xe6\x9c\xac\xe3\x81\xa7\xe4\xbd\x9c\xe3\x82\x89\xe3\x82\x8c\xe3\x81\x9f\xe8\x8b\xb1\xe8\xaa\x9e\xe9\xa2\xa8\xe3\x81\xae\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\xe8\xaa\x9e\xe5\xbd\x99\xe3\x81\xae\xe3\x81\x93\xe3\x81\xa8\xe3\x81\xa7\xe3\x81\x82\xe3\x82\x8b\xe3\x80\x82"\np=re.compile(u\'(\'+ KEYWORD +u\')(?!(([^<>]*?)>)|([^>]*?</a>))\',re.UNICODE)\noutput=p.sub(u\'<a href="\'+ URL +\'">\\1</a>\',content)\n
Run Code Online (Sandbox Code Playgroud)\n\n

正则表达式和 p.sub 工作正常,但反向引用\\1不起作用!的输出\\1是这样的:\xe1\x9e\x93

\n\n

我尝试修改代码,encode(\'utf-8\')但结果是一样的:

\n\n
p=re.compile(u\'(\'+ KEYWORD +u\')(?!(([^<>]*?)>)|([^>]*?</a>))\'.encode(\'utf-8\'),re.UNICODE)\noutput=p.sub(u\'<a href="\'+ URL +\'">\\1</a>\'.encode(\'utf-8\'),content.encode(\'utf-8\'))\n
Run Code Online (Sandbox Code Playgroud)\n\n

谁能告诉我出了什么问题吗?

\n

python regex unicode utf-8

2
推荐指数
1
解决办法
1043
查看次数

html中&lt;p&gt;标签中的单引号和双引号是否需要转义?

我刚刚安装了这个所见即所得文本编辑器,我发现<p></p>当我查看 html 源代码时,它不会转义标签中的单引号和双引号。

演示: http: //files.wymeditor.org/wymeditor/examples/01-basic.html

亲自尝试一下。

我想知道是否有一些文件可以澄清这个问题。有必要吗?

html wysiwyg rich-text-editor

-1
推荐指数
1
解决办法
2338
查看次数