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.

Le jeu. 16 nov. 2023 à 20:25, Oberdan Santos <sc.ober...@gmail.com> a
écrit :

> 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'
>
>
> #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)
>
> .................................................................
>
> #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.
> ......
> Em quinta-feira, 16 de novembro de 2023 às 12:00:38 UTC-3, Laurent Daverio
> escreveu:
>
>> 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.
>>
>> Le jeu. 16 nov. 2023 à 14:52, Oberdan Santos <sc.ob...@gmail.com> a
>> écrit :
>> >
>> > 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.
>> >
>> > --
>> > 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.
>>
>>
> --
> 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-discuss+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/5f8e5a02-858e-4abb-b582-994d780b5b63n%40googlegroups.com
> <https://groups.google.com/d/msgid/pylons-discuss/5f8e5a02-858e-4abb-b582-994d780b5b63n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6wmMPy4yrkAVAoKWcRCTUCaNF-6j7RTDg-gnvz3ZrctjQ%40mail.gmail.com.

Reply via email to