Skip to content

Commit 26549fb

Browse files
Built site for gh-pages
1 parent 78ccc5c commit 26549fb

File tree

5 files changed

+112
-17
lines changed

5 files changed

+112
-17
lines changed

.nojekyll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0d8f0b6c
1+
f3f0e57a

docs/architecture.html

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ <h2 id="toc-title">On this page</h2>
140140

141141
<ul>
142142
<li><a href="#data-flow" id="toc-data-flow" class="nav-link active" data-scroll-target="#data-flow">Data flow</a></li>
143+
<li><a href="#form-validation-flow" id="toc-form-validation-flow" class="nav-link" data-scroll-target="#form-validation-flow">Form validation flow</a>
144+
<ul class="collapse">
145+
<li><a href="#objectives" id="toc-objectives" class="nav-link" data-scroll-target="#objectives">Objectives</a></li>
146+
<li><a href="#obstacles" id="toc-obstacles" class="nav-link" data-scroll-target="#obstacles">Obstacles</a></li>
147+
<li><a href="#approaches" id="toc-approaches" class="nav-link" data-scroll-target="#approaches">Approaches</a></li>
148+
</ul></li>
143149
</ul>
144150
</nav>
145151
</div>
@@ -175,8 +181,99 @@ <h2 class="anchored" data-anchor-id="data-flow">Data flow</h2>
175181
</figure>
176182
</div>
177183
<p>The advantage of the PRG pattern is that it is very straightforward to implement and keeps most of the rendering logic on the server side. The disadvantage is that it requires an extra round trip to the database to fetch the updated data, and re-rendering the entire page template may be less efficient than a partial page update on the client side.</p>
184+
</section>
185+
<section id="form-validation-flow" class="level2">
186+
<h2 class="anchored" data-anchor-id="form-validation-flow">Form validation flow</h2>
187+
<p>We’ve experimented with several approaches to validating form inputs in the FastAPI endpoints.</p>
188+
<section id="objectives" class="level3">
189+
<h3 class="anchored" data-anchor-id="objectives">Objectives</h3>
190+
<p>Ideally, on an invalid input, we would redirect the user back to the form, preserving their inputs and displaying an error message about which input was invalid.</p>
191+
<p>This would keep the error handling consistent with the PRG pattern described in the <a href="https://promptlytechnologies.com/fastapi-jinja2-postgres-webapp/docs/architecture">Architecture</a> section of this documentation.</p>
192+
<p>To keep the code DRY, we’d also like to handle such validation with Pydantic dependencies, Python exceptions, and exception-handling middleware as much as possible.</p>
193+
</section>
194+
<section id="obstacles" class="level3">
195+
<h3 class="anchored" data-anchor-id="obstacles">Obstacles</h3>
196+
<p>One challenge is that if we redirect back to the page with the form, the page is re-rendered with empty form fields.</p>
197+
<p>This can be overcome by passing the inputs from the request as context variables to the template.</p>
198+
<p>But that’s a bit clunky, because then we have to support form-specific context variables in every form page and corresponding GET endpoint.</p>
199+
<p>Also, we have to:</p>
200+
<ol type="1">
201+
<li>access the request object (which is not by default available to our middleware), and</li>
202+
<li>extract the form inputs (at least one of which is invalid in this error case), and</li>
203+
<li>pass the form inputs to the template (which is a bit challenging to do in a DRY way since there are different sets of form inputs for different forms).</li>
204+
</ol>
205+
<p>Solving these challenges is possible, but gets high-complexity pretty quickly.</p>
206+
</section>
207+
<section id="approaches" class="level3">
208+
<h3 class="anchored" data-anchor-id="approaches">Approaches</h3>
209+
<p>The best solution, I think, is to use really robust client-side form validation to prevent invalid inputs from being sent to the server in the first place. That makes it less important what we do on the server side, although we still need to handle the server-side error case as a backup in the event that something slips past our validation on the client side.</p>
210+
<p>Here are some patterns we’ve considered for server-side error handling:</p>
211+
<table class="caption-top table">
212+
<colgroup>
213+
<col style="width: 6%">
214+
<col style="width: 12%">
215+
<col style="width: 22%">
216+
<col style="width: 22%">
217+
<col style="width: 21%">
218+
<col style="width: 14%">
219+
</colgroup>
220+
<thead>
221+
<tr class="header">
222+
<th>ID</th>
223+
<th>Approach</th>
224+
<th>Returns to same page</th>
225+
<th>Preserves form inputs</th>
226+
<th>Follows PRG pattern</th>
227+
<th>Complexity</th>
228+
</tr>
229+
</thead>
230+
<tbody>
231+
<tr class="odd">
232+
<td>1</td>
233+
<td>Validate with Pydantic dependency, catch and redirect from middleware (with exception message as context) to an error page with “go back” button</td>
234+
<td>No</td>
235+
<td>Yes</td>
236+
<td>Yes</td>
237+
<td>Low</td>
238+
</tr>
239+
<tr class="even">
240+
<td>2</td>
241+
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param</td>
242+
<td>Yes</td>
243+
<td>No</td>
244+
<td>Yes</td>
245+
<td>Medium</td>
246+
</tr>
247+
<tr class="odd">
248+
<td>3</td>
249+
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param and form inputs as context</td>
250+
<td>Yes</td>
251+
<td>Yes</td>
252+
<td>Yes</td>
253+
<td>High</td>
254+
</tr>
255+
<tr class="even">
256+
<td>4</td>
257+
<td>Validate with Pydantic dependency, use session context to get form inputs from request, redirect to origin page from middleware with exception message and form inputs as context so we can re-render page with original form inputs</td>
258+
<td>Yes</td>
259+
<td>Yes</td>
260+
<td>Yes</td>
261+
<td>High</td>
262+
</tr>
263+
<tr class="odd">
264+
<td>5</td>
265+
<td>Validate in either Pydantic dependency or function endpoint body and directly return error message in JSON, then mount it with HTMX or some simple layout-level Javascript</td>
266+
<td>Yes</td>
267+
<td>Yes</td>
268+
<td>No</td>
269+
<td>Low</td>
270+
</tr>
271+
</tbody>
272+
</table>
273+
<p>Presently this template primarily uses option 1 but also supports option 2. Ultimately, I think option 5 will be preferable; support for that <a href="https://github.com/Promptly-Technologies-LLC/fastapi-jinja2-postgres-webapp/issues/5">is planned</a> for a future update or fork of this template.</p>
178274

179275

276+
</section>
180277
</section>
181278

182279
</main> <!-- /main -->

docs/customization.html

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,8 @@
134134
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
135135
<!-- sidebar -->
136136
<!-- margin-sidebar -->
137-
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
138-
<nav id="TOC" role="doc-toc" class="toc-active">
139-
<h2 id="toc-title">On this page</h2>
140-
141-
<ul>
142-
<li><a href="#under-construction" id="toc-under-construction" class="nav-link active" data-scroll-target="#under-construction">Under construction</a></li>
143-
</ul>
144-
</nav>
137+
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar zindex-bottom">
138+
145139
</div>
146140
<!-- main -->
147141
<main class="content" id="quarto-document-content">
@@ -165,11 +159,8 @@ <h1 class="title">Customization</h1>
165159
</header>
166160

167161

168-
<section id="under-construction" class="level2">
169-
<h2 class="anchored" data-anchor-id="under-construction">Under construction</h2>
170162

171163

172-
</section>
173164

174165
</main> <!-- /main -->
175166
<script id="quarto-html-after-body" type="application/javascript">

search.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
"section": "",
7777
"text": "This application uses a Post-Redirect-Get (PRG) pattern. The user submits a form, which sends a POST request to a FastAPI endpoint on the server. The database is updated, and the user is redirected to a GET endpoint, which fetches the updated data and re-renders the Jinja2 page template with the new data.\n\n\n\nData flow diagram\n\n\nThe advantage of the PRG pattern is that it is very straightforward to implement and keeps most of the rendering logic on the server side. The disadvantage is that it requires an extra round trip to the database to fetch the updated data, and re-rendering the entire page template may be less efficient than a partial page update on the client side."
7878
},
79+
{
80+
"objectID": "docs/architecture.html#form-validation-flow",
81+
"href": "docs/architecture.html#form-validation-flow",
82+
"title": "Architecture",
83+
"section": "Form validation flow",
84+
"text": "Form validation flow\nWe’ve experimented with several approaches to validating form inputs in the FastAPI endpoints.\n\nObjectives\nIdeally, on an invalid input, we would redirect the user back to the form, preserving their inputs and displaying an error message about which input was invalid.\nThis would keep the error handling consistent with the PRG pattern described in the Architecture section of this documentation.\nTo keep the code DRY, we’d also like to handle such validation with Pydantic dependencies, Python exceptions, and exception-handling middleware as much as possible.\n\n\nObstacles\nOne challenge is that if we redirect back to the page with the form, the page is re-rendered with empty form fields.\nThis can be overcome by passing the inputs from the request as context variables to the template.\nBut that’s a bit clunky, because then we have to support form-specific context variables in every form page and corresponding GET endpoint.\nAlso, we have to:\n\naccess the request object (which is not by default available to our middleware), and\nextract the form inputs (at least one of which is invalid in this error case), and\npass the form inputs to the template (which is a bit challenging to do in a DRY way since there are different sets of form inputs for different forms).\n\nSolving these challenges is possible, but gets high-complexity pretty quickly.\n\n\nApproaches\nThe best solution, I think, is to use really robust client-side form validation to prevent invalid inputs from being sent to the server in the first place. That makes it less important what we do on the server side, although we still need to handle the server-side error case as a backup in the event that something slips past our validation on the client side.\nHere are some patterns we’ve considered for server-side error handling:\n\n\n\n\n\n\n\n\n\n\n\nID\nApproach\nReturns to same page\nPreserves form inputs\nFollows PRG pattern\nComplexity\n\n\n\n\n1\nValidate with Pydantic dependency, catch and redirect from middleware (with exception message as context) to an error page with “go back” button\nNo\nYes\nYes\nLow\n\n\n2\nValidate in FastAPI endpoint function body, redirect to origin page with error message query param\nYes\nNo\nYes\nMedium\n\n\n3\nValidate in FastAPI endpoint function body, redirect to origin page with error message query param and form inputs as context\nYes\nYes\nYes\nHigh\n\n\n4\nValidate with Pydantic dependency, use session context to get form inputs from request, redirect to origin page from middleware with exception message and form inputs as context so we can re-render page with original form inputs\nYes\nYes\nYes\nHigh\n\n\n5\nValidate in either Pydantic dependency or function endpoint body and directly return error message in JSON, then mount it with HTMX or some simple layout-level Javascript\nYes\nYes\nNo\nLow\n\n\n\nPresently this template primarily uses option 1 but also supports option 2. Ultimately, I think option 5 will be preferable; support for that is planned for a future update or fork of this template."
85+
},
7986
{
8087
"objectID": "docs/authentication.html",
8188
"href": "docs/authentication.html",

sitemap.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
</url>
1111
<url>
1212
<loc>https://Promptly-Technologies-LLC.github.io/fastapi-jinja2-postgres-webapp/docs/contributing.html</loc>
13-
<lastmod>2024-11-21T16:47:21.891Z</lastmod>
13+
<lastmod>2024-11-21T16:55:02.425Z</lastmod>
1414
</url>
1515
<url>
1616
<loc>https://Promptly-Technologies-LLC.github.io/fastapi-jinja2-postgres-webapp/docs/architecture.html</loc>
17-
<lastmod>2024-11-19T23:41:34.445Z</lastmod>
17+
<lastmod>2024-11-23T16:49:49.060Z</lastmod>
1818
</url>
1919
<url>
2020
<loc>https://Promptly-Technologies-LLC.github.io/fastapi-jinja2-postgres-webapp/docs/authentication.html</loc>
21-
<lastmod>2024-11-21T16:35:23.903Z</lastmod>
21+
<lastmod>2024-11-21T16:55:02.394Z</lastmod>
2222
</url>
2323
<url>
2424
<loc>https://Promptly-Technologies-LLC.github.io/fastapi-jinja2-postgres-webapp/docs/customization.html</loc>
25-
<lastmod>2024-11-19T23:41:34.470Z</lastmod>
25+
<lastmod>2024-11-23T16:50:42.867Z</lastmod>
2626
</url>
2727
<url>
2828
<loc>https://Promptly-Technologies-LLC.github.io/fastapi-jinja2-postgres-webapp/docs/installation.html</loc>
29-
<lastmod>2024-11-21T16:52:57.906Z</lastmod>
29+
<lastmod>2024-11-21T16:58:31.569Z</lastmod>
3030
</url>
3131
</urlset>

0 commit comments

Comments
 (0)