# How to use flask\_tryton

**URL:** <https://discuss.tryton.org/t/how-to-use-flask-tryton/1266>\
**Category:** Developer\
**Tags:** flask\
**Created:** [March 25, 2019, 7:27pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266 "2019-03-25T19:27:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [March 25, 2019, 7:27pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/1 "2019-03-25T19:27:20Z")

</div>

Hi,

I was trying to make a flask app that send info to tryton using flask\_tryton.

The code is the follow:

```
from flask import render_template, flash, redirect, url_for, session
from app import app
from app import tryton 

Party = tryton.pool.get('party.party')

@tryton.default_context
def default_context():
	return User.get_preferences(context_only=True) 

@app.route('/', methods=['GET', 'POST'])
@tryton.transaction(readonly=False)
def index():
    form = LoginForm() 
	if form.validate_on_submit():
		firstname = form.firstname.data
		party, = Party.create([{
			'name': firstname,
			}])

```

I have already setting these variables:

```
    TRYTON_DATABASE: the Tryton’s database to connect.
    TRYTON_USER: the Tryton user id to use, by default 0 (aka root).
    TRYTON_CONFIG: the optional path to the Tryton’s configuration.

```

But when I send the info I receive this error:

```
  File "/testlib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/testlib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/testlib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/testlib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/testlib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/testlib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/testlib/python3.5/site-packages/flask_tryton.py", line 97, in wrapper
    with Transaction().start(database, 0):
  File "/testlib/python3.5/site-packages/trytond/transaction.py", line 96, in start
    autocommit=autocommit)
  File "/testlib/python3.5/site-packages/trytond/backend/postgresql/database.py", line 158, in get_connection
    conn.set_isolation_level(ISOLATION_LEVEL_REPEATABLE_READ)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 84, in check_async_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 273, in set_isolation_level
    prev = self.isolation_level
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 43, in check_closed_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 265, in isolation_level
    name = self._get_guc('default_transaction_isolation')
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 234, in _get_guc
    raise exceptions.OperationalError("can't fetch %s" % name)
psycopg2cffi._impl.exceptions.OperationalError: can't fetch default_transaction_isolation

```

I’m not sure what thing I miss something but I based the code in [flask-tryton · PyPI](https://pypi.org/project/flask_tryton/)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)\
**Post date:** [March 26, 2019, 10:29am UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/2 "2019-03-26T10:29:43Z")

</div>

Do you have the same issue with `psycopg2` (and not `psycopg2cffi`) ?

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [March 27, 2019, 7:39pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/3 "2019-03-27T19:39:36Z")

</div>

Well, I don’t use psycopg2 'cause can run trytond with it, I only have psycopg2cffi for database usage, it works fine, so I’m not sure if I need some special config with flask\_tryton?

---

<div class="post-metadata">

**Author:** ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)\
**Post date:** [March 28, 2019, 1:30pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/4 "2019-03-28T13:30:30Z")

</div>

There is no special configuration regarding the database for `flask_tryton` except the variables you’re setting. That’s why the most probable issue look like `psycopg2cffi`, it would be interesting anyway to understand why you can’t run tryton with `psycopg2` (are you using pypy ?) and it would help to rule out an eventual issue in flask\_tryton.

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [March 28, 2019, 6:14pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/5 "2019-03-28T18:14:06Z")

</div>

I use ubuntu or debian for diffent apps, but in no one I can use psycopg2, at the moment I don’t have an instance running without psycopg2 but the most common trouble log is with isolation transaction.I use pypi to to install flask\_tryton, I’m trying to migrate to docker but first I need to see that it works fine in a tradicional enviroment.

I couldn’t not find the code of flask\_tryton but I create one on [GitHub - iehoshia/Flask](http://github.com/iehoshia/flask), so maybe I miss some config according to [https://github.com/iehoshia/flask\_tryton/blob/master/flask\_tryton.py#L69](https://github.com/iehoshia/flask_tryton/blob/master/flask_tryton.py#L69).

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)\
**Post date:** [March 28, 2019, 9:17pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/6 "2019-03-28T21:17:17Z")

</div>

I am sorry but I don’t understand a word you say. Do you or do you not use psycopg2 ?

Also pypi ≠ pypy. The first one is the python package index, the second one is an implementation of the python interpreter in python.

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [March 28, 2019, 11:00pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/7 "2019-03-28T23:00:10Z")

</div>

Well, I have install psycopg and psycopg2cffi 'cause without it tryton does not run, I don’t know why. Well, I don’t use pypy.

---

<div class="post-metadata">

**Author:** ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)\
**Post date:** [March 29, 2019, 10:03am UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/8 "2019-03-29T10:03:53Z")

</div>

> [@iehoshia](#):
>
> Well, I have install psycopg and psycopg2cffi 'cause without it tryton does not run, I don’t know why

This is really strange it shouldn’t happen. You have messed up something in your installation.

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [April 25, 2019, 10:57pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/9 "2019-04-25T22:57:22Z")

</div>

I need to add the follow lines to the route, after it works well.  
@app.route(‘/’, methods=[‘GET’, ‘POST’])  
@tryton.transaction(readonly=False,user=1)

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [May 16, 2019, 10:09pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/10 "2019-05-16T22:09:02Z")

</div>

After some time, the previous message make it works, but sometimes the app downs, so I uninstall psycopg2cffi and try the follow post [https://stackoverflow.com/questions/22752521/uwsgi-flask-sqlalchemy-and-postgres-ssl-error-decryption-failed-or-bad-reco/22753269#22753269](https://stackoverflow.com/questions/22752521/uwsgi-flask-sqlalchemy-and-postgres-ssl-error-decryption-failed-or-bad-reco/22753269#22753269), it works fine for me now.

```auto
# the fix
lazy = true
lazy-apps = true

```

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [June 15, 2019, 10:09pm UTC](https://discuss.tryton.org/t/how-to-use-flask-tryton/1266/11 "2019-06-15T22:09:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
