REF. Problem registering data in the bank

45 views
Skip to first unread message

Oberdan Santos

unread,
Nov 16, 2023, 8:51:54 AM11/16/23
to pylons-discuss
Hello!! I am facing a problem when trying to post data to the database. When asking to send the application data I receive the following: Squashed pyramid.httpexceptions.HTTPNotFound at http://localhost:6543/mod_plataforma/mod1_plataforma/recepx/$%7Bsave_url%7D

. Below is information about my files.

# piprdc/piprdc/routes.py
def includeme(config):
     config.add_static_view('static', 'static', cache_max_age=3600)
     config.add_route('home', '/')
     config.add_route('modulo_x', '/mod_plataforma')
     config.add_route('modulo1', '/mod_plataforma/mod1_plataforma')
     config.add_route('modulo2', '/mod_plataforma/mod2_plataforma')
     config.add_route('modulo3', '/mod_plataforma/mod3_plataforma')
     config.add_route('modulo4', '/mod_plataforma/mod4_plataforma')
     config.add_route('modulo5', '/mod_plataforma/mod5_plataforma')
     config.add_route('modulo6', '/mod_plataforma/mod6_plataforma')
     config.add_route('abcx', '/mod_plataforma/mod1_plataforma/recepx')
     config.add_route('add_reg', '/mod_plataforma/mod1_plataforma/recepx/add_reg')
     config.add_route('query', '/mod_plataforma/mod1_plataforma/recepx/query')

..............................

#views/default.py
from pyramid.view import view_config
from pyramid.response import Response
from sqlalchemy.exc import SQLAlchemyError, DBAPIError
from pyramid.httpexceptions import HTTPFound
from .. import models
from ..models import Patient
from ..models import get_tm_session
dbsession = get_tm_session

view_config(route_name='add_reg', renderer='piprdc:templates/reg_pacx.jinja2')
def add_reg(request):
     save_url = request.route_url('add_reg')
     request.route_url('query')
     print('RUN')
     if request.params:
         id=request.params['id']
         name=request.params['name']
         age=request.params['age']
         birth_date=request.params['birth_date']
         sex=request.params['sex']
         race=request.params['race']
         fone=request.params['fone']
         address=request.params['address']
         cpf=request.params['cpf']
         cns=request.params['cns']
         patients=Patient(id=id, name=name, age=age, date_birth=date_birth, sex=sex,
                            race=race, phone=phone, address=address, cpf=cpf, cns=cns)
         try:
             dbsession.add_reg(patients)
             return HTTPFound(location=request.route_url('query'))
         except DBAPIError:
             return Response("DB ERROR")
     else:
         print('DO NOT RUN')
         return{'save_url': save_url, 'project': 'piprdc'}

........................................

#templates/reg_pacx.jinja2
{% extends "basefull.jinja2" %}
{% block container %}
<html>
<body>
    <h1><span class="font-semi-bold">Patient registration</span></h1>
    <form action="${save_url}" method="POST">
    <p>Patient Id: <input type="Integer" name="id"/> </p>
    <p>Patient Name: <input type="text" name="name"/> </p>
    <p>Age: <input type="Integer" name="age"/> </p>
    <p>Date of birth: <input type="Integer" name="date_birth"/> </p>
    <p>Gender: <input type="text" name="sex"/> </p>
    <p>Race: <input type="text" name="breed"/> </p>
    <p>Phone: <input type="text" name="phone"/> </p>
    <p>Address: <input type="text" name="address"/> </p>
    <p>CPF: <input type="Integer" name="cpf"/> </p>
    <p>CNS: <input type="Integer" name="cns"/> </p>
    <p><input type="submit" value="Submit"> </p>
</body>
</html>
{% endblock container %}

................................
#models/__init__.py

from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
from sqlalchemy_continuum.plugins import Plugin
import zope.sqlalchemy

from .mymodel import Patient # flake8: noqa

configure_mappers()

def get_engine(settings, prefix='sqlalchemy.'):
     return engine_from_config(settings, prefix)

def get_session_factory(engine):
     factory = sessionmaker()
     factory.configure(bind=engine)
     return factory

def get_tm_session(session_factory, transaction_manager, request=None):
dbsession = session_factory(info={"request": request})
     zope.sqlalchemy.register(
         dbsession, transaction_manager=transaction_manager
     )
     return dbsession
def includeme(config):
   
     settings = config.get_settings()
     settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
   
      config.include('pyramid_tm')

     config.include('pyramid_retry')

     # hook to share the dbengine fixture in testing
     dbengine = settings.get('dbengine')
     if not dbengine:
         dbengine = get_engine(settings)

     session_factory = get_session_factory(dbengine)
     config.registry['dbsession_factory'] = session_factory
   
     def dbsession(request):
         dbsession = request.environ.get('app.dbsession')
         if dbsession is None:
              dbsession = get_tm_session(
                 session_factory, request.tm, request=request
      config.add_request_method(dbsession, reify=True)

Any help is welcome.

Laurent Daverio

unread,
Nov 16, 2023, 10:00:38 AM11/16/23
to pylons-...@googlegroups.com
Hello, don't you have, by any chance, a URL like this in your frontend?

'/mod_plataforma/mod1_plataforma/recepx/${save_url}'

If so, you should replace the simple quotes with backticks:

`/mod_plataforma/mod1_plataforma/recepx/${save_url}`

To me, it looks like a Javascript typo, not a Pyramid error.

Laurent.
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/6c951fdb-95ea-440f-b3f7-81f93845dacfn%40googlegroups.com.

Oberdan Santos

unread,
Nov 16, 2023, 2:25:36 PM11/16/23
to pylons-discuss
Sua dica esta correta.  Fiz algumas alterações e percebi avanços. No entanto estou com o seguinte erro:
File "C:\Users\oberd\proj_piprdc\piprdc\piprdc\views\default.py", line 87, in add
    dbsession.add(pacientes)
    ^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'add'
.................................................................

#views/default.py
from pyramid.view import view_config
from pyramid.response import Response
from sqlalchemy.exc import SQLAlchemyError
from pyramid.httpexceptions import HTTPFound
from ..models import Paciente
from ..models import get_tm_session
dbsession = get_tm_session

@view_config(route_name='add', request_method='POST')
def add(request):
    id=request.POST['id']
    name=request.POST['name']
    idade=request.POST['idade']
    data_nascimento=request.POST['data_nascimento']
    sexo=request.POST['sexo']
    raca=request.POST['raca']
    fone=request.POST['fone']
    endereco=request.POST['endereco']
    cpf=request.POST['cpf']
    cns=request.POST['cns']
    pacientes=Paciente(id=id, name=name, idade=idade, data_nascimento=data_nascimento, sexo=sexo,
                           raca=raca, fone=fone, endereco=endereco, cpf=cpf, cns=cns)
    dbsession.add(pacientes)
    dbsession.commit()
    return HTTPFound(location='http://localhost:6543/')

Já fiz algumas alterações, mas sem sucesso.
......

Laurent Daverio

unread,
Nov 16, 2023, 3:11:35 PM11/16/23
to pylons-...@googlegroups.com
I think you want to write "request.dbsession.add(...)" instead.

Function dbsession is not meant to be used directly, only to create "request.dbsession", via "config.add_request_method(dbsession, reify=True)". This way, in your code, you never have to declare or import "dbsession", just use "request.dbsession" directly.

Laurent.

Oberdan Santos

unread,
Nov 16, 2023, 3:48:49 PM11/16/23
to pylons-discuss
The new structure advanced, but presented another error that I don't know where to start and resolve it.

@view_config(route_name='add', request_method='POST')
def add(request):
    id=request.POST['id']
    name=request.POST['name']
    idade=request.POST['idade']
    data_nascimento=request.POST['data_nascimento']
    sexo=request.POST['sexo']
    raca=request.POST['raca']
    fone=request.POST['fone']
    endereco=request.POST['endereco']
    cpf=request.POST['cpf']
    cns=request.POST['cns']
    pacientes=Paciente(id=id, name=name, idade=idade, data_nascimento=data_nascimento, sexo=sexo, raca=raca, fone=fone, endereco=endereco, cpf=cpf, cns=cns)
    request.dbsession.add(pacientes)
    request.dbsession.commit()
    return HTTPFound(location='http://localhost:6543/lista')

ERRO:
  File "C:\Users\oberd\proj_piprdc\oberd\Lib\site-packages\zope\sqlalchemy\datamanager.py", line 333, in before_commit
    or self.transaction_manager.get().status == ZopeStatus.COMMITTING
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Transaction must be committed using the transaction manager

Florian Schulze

unread,
Nov 16, 2023, 3:52:03 PM11/16/23
to pylons-discuss

With Jinja Templates you have to use {{ save_url }} not ${save_url}. You can see from the URL the form is trying to POST to that it contains the $ sign. You can also see such things from your Browser HTML Inspector.

Regards,
Florian Schulze

Oberdan Santos

unread,
Nov 16, 2023, 3:53:59 PM11/16/23
to pylons-discuss
Obrigado!!!

Laurent Daverio

unread,
Nov 16, 2023, 4:19:06 PM11/16/23
to pylons-...@googlegroups.com
Yes, that's true. If you write a monolithic app with Jinja templates, you will use {{ }}. If you use a Javascript frontend, you will find that `${}` is very convenient :)

Laurent Daverio

unread,
Nov 16, 2023, 4:24:10 PM11/16/23
to pylons-...@googlegroups.com
Maybe you should take the time to follow the official Pyramid tutorials, they would answer many of your questions. 

In common Pyramid apps (including yours, I suppose), SQL transactions are controlled by HTTP "transactions", which is extremely convenient: if the view fails, the SQL transaction is rolled back. If the view succeeds, the SQL transaction is committed automatically. You never write request.dbsession.commit().

Please, please, take the time to read the tutorials. I'm willing to help, but I think this list is not the right place for debugging your app.

Oberdan Santos

unread,
Nov 16, 2023, 4:24:43 PM11/16/23
to pylons-discuss
Sim!!  importante dica.

Oberdan Santos

unread,
Nov 16, 2023, 4:37:38 PM11/16/23
to pylons-discuss
I have done a lot of reading, some of it with tutorials, but very superficial.
I have some scripts with a database without using http transactions, which work normally.
Any suggestions for someone who could be helping me debug the code or even yourself and how I can pay for it?

Anyway, thanks for the support.

Steve Piercy

unread,
Nov 16, 2023, 4:43:03 PM11/16/23
to pylons-...@googlegroups.com

Oberdan Santos

unread,
Nov 16, 2023, 4:45:23 PM11/16/23
to pylons-discuss
Obrigado!!!!
Reply all
Reply to author
Forward
0 new messages