On Thu, Nov 23, 2023 at 11:50 AM Oberdan Santos <sc.ober...@gmail.com> wrote:
>
> You should note in the subject statement that in addition to the query, I 
> have the problem of the result being published on another page.
> query page code
> # templates/pac_recepx.jinja2
>
> <div class="form">
>         <div class="row g-2 mt-3">
>             <h3><span class="font-semi-bold">Consultar cadastro do 
> paciente</span></h3>
>         </div>
>         <form class="form-inline my-2 my-lg-0" 
> action="http://localhost:6543/queryx"; method="GET">
>             <label for="cpf">Digite o CPF (11 números)</label>
>             <input class="form-control mr-sm-2" type="text" id="cpf" 
> name="cpf" required maxlength="11" value=''>
>             <button class="btn btn-outline-success my-2 my-sm-0" 
> type="submit">Consultar</button>
>         </form>
>     </div>
>
> The result is going to...
> action="http://localhost:6543/queryx
>
> How do I take this result to the same page as the query 
> (templates/pac_recepx.jinja2), that is, place it below the query?

There are two approaches.

SERVER-SIDE ONLY:

Remove the form `action` attribute. The form will post back to the
same view that contained the form. In the view, add an `if` stanza to
distinguish whether there's form input or not:

```
cpf = request.params.get("cpf", "")   # User input, or "" if no input.
error = ""                                          # Validation error
message, or None if no error.
rows = None                                   # Result rows, or None
if no valid input, or [] if valid input but zero results.
if cpf:   # If value is not "" or None.
    if CPF_IS_VALID:
        rows = request.dbsession...
    else:
        error = "Input is invalid."
return {"cpf": cpf, "error": error, "rows": rows}
```

Then your template might always show the form, but only show the
results section if there was input, and only show the results table if
there was at least one result, and only show the error message if
there was a user error. I use Mako templating so I'll write it that
way.

```
## page.mako
<form method="GET" class="...">
    % if error:
    <div>${error}</div>
    %endif
    <input name="cpf" value="${cpf}" ... />
</form>

% if results is not None:    # If there was valid user input.
<h2>Results</h2>
% if results:    # If there was at least one result.
<table>
    <th>Header</th>...
% for r in results:
    <tr>...</tr>
% endfor
% else:   # Else there were zero results.
<p><em>No results.</em></p>
% endif
```

CLIENT-SIDE ALTERNATIVE:

Write Javascript to intercept the Submit click. Send an AJAX request
to the server to get the results in a JSON array. Use Javascript to
populate the results table. That's beyond the scope of this mailing
list. In this case you'd have a view that processes the AJAX request
and converts the rows list to JSON before returning it.

-- 
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/CAH9f%3DuotPq%3D-%2Boojik5HLXWY%3Dg7QX3k7SAx5nZ%3D2v1g8n7GdiA%40mail.gmail.com.

Reply via email to