Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中散列路由?_Python_Flask_Hash_Routes_Flask Sqlalchemy - Fatal编程技术网

如何在Python中散列路由?

如何在Python中散列路由?,python,flask,hash,routes,flask-sqlalchemy,Python,Flask,Hash,Routes,Flask Sqlalchemy,我的目标很简单,我创建了一个接收ID的路由 @app.route('/afbase/<int:pid>', methods=["GET", "PATCH", "DELETE"]) # We defined the page that will retrieve some info def show(pid): new_person = People.query.filter_by(pid=pid).first() @app.route('/afbase/',方法=[“获取”

我的目标很简单,我创建了一个接收ID的路由

@app.route('/afbase/<int:pid>', methods=["GET", "PATCH", "DELETE"])
# We defined the page that will retrieve some info
def show(pid):
    new_person = People.query.filter_by(pid=pid).first()
@app.route('/afbase/',方法=[“获取”、“修补”、“删除”])
#我们定义了将检索某些信息的页面
def显示(pid):
new_person=People.query.filter_by(pid=pid.first)()
我不希望将此ID显示给最终用户。如何散列路由或部分散列路由

此路由将接收一个名为PID的变量,如您所见,此PID变量可被视为ID。不区分大小写,但不能在浏览器URL上显示


我尝试使用hashlib,但没有取得多大成功。

您可以使用
hashid
库对整数ID进行编码和解码。首先,
pip安装hashids
。然后,创建一些实用函数

# utils.py
from flask import current_app
from hashids import Hashids

def create_hashid(id):
    hashids = Hashids(min_length=5, salt=current_app.config['SECRET_KEY'])
    hashid = hashids.encode(id)
    return hashid

def decode_hashid(hashid):
    hashids = Hashids(min_length=5, salt=current_app.config['SECRET_KEY'])
    id = hashids.decode(hashid)
    return id
接下来,创建一个全局环境变量,以便可以从jinja2模板调用create_hashid函数:

# app.py
from utils import create_hashid
app.jinja_env.globals.update(create_hashid=create_hashid)
# index.html
<a href="{{ url_for('invoice.view_invoice', hashid=create_hashid(invoice.id) ) }}">View Invoice</a>
下面是如何从模板中的链接调用该函数:

# app.py
from utils import create_hashid
app.jinja_env.globals.update(create_hashid=create_hashid)
# index.html
<a href="{{ url_for('invoice.view_invoice', hashid=create_hashid(invoice.id) ) }}">View Invoice</a>
#index.html
最后,查看功能:

# views.py
@blueprint.route('/dashboard/invoice/<hashid>')
@login_required
def view_invoice(hashid):
    invoice_id = decode_hashid(hashid)
    invoice = Invoice.query.filter_by(
        user_id=current_user.id,
        id=invoice_id
    ).first_or_404()

return render_template(
    'dashboard/invoice/view_invoice.html',
    invoice=invoice
)
#views.py
@blueprint.route(“/dashboard/invoice/”)
@需要登录
def view_发票(哈希ID):
发票\u id=解码\u哈希id(哈希id)
发票=invoice.query.filter\u by(
user\u id=当前的\u user.id,
id=发票\u id
)。首先
返回渲染模板(
“dashboard/invoice/view_invoice.html”,
发票=发票
)

为什么不使用散列url创建另一个方法(使用
@app.route
decorator),该方法将使用真实id调用真实函数?散列是不可逆的。你希望如何获得客户的PID?@Keith我是向他发送独家视图的人,我不需要来自最终客户的信息,我正在数据库中为每个客户显示一个动态视图。因为这个信息将是公开的(而且必须是因为我想让它尽可能简单),所以。。。我将向他发送一个包含此特定pid的url。问题是,其他客户可以在url中输入一个id,然后访问其他客户区域,因此我希望对我打印的url进行哈希处理。我希望这是有意义的,我是一个2个月的学习者。顺便说一句:如果有一个很好的理由你不希望他们看到彼此的数据,那么这肯定是不够的保护。为每个客户存储UUID会更好,但仍然不好;你需要某种真正的身份验证,如果它有点重要的话。而且,
first()
意味着可以得到多个结果
one()
更合适,因为该标识符应该是唯一的,如果由于某种原因它不是唯一的,则重复的标识符将导致尝试访问时出错(因此具有重复标识符的用户都可能看不到其他用户的数据)。