You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
62
+
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. One 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. Another disadvantage is that it if the user makes an invalid form submission, they will see an error page and will have to click the browser's "back" button to get back to the form with their original form inputs.
63
63
64
-
## Form validation flow
65
-
66
-
We've experimented with several approaches to validating form inputs in the FastAPI endpoints.
67
-
68
-
### Objectives
69
-
70
-
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.
71
-
72
-
This would keep the error handling consistent with the PRG pattern described in the [Architecture](https://promptlytechnologies.com/fastapi-jinja2-postgres-webapp/docs/architecture) section of this documentation.
73
-
74
-
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.
75
-
76
-
### Obstacles
77
-
78
-
One challenge is that if we redirect back to the page with the form, the page is re-rendered with empty form fields.
79
-
80
-
This can be overcome by passing the inputs from the request as context variables to the template.
81
-
82
-
But that's a bit clunky, because then we have to support form-specific context variables in every form page and corresponding GET endpoint.
83
-
84
-
Also, we have to:
85
-
86
-
1. access the request object (which is not by default available to our middleware), and
87
-
2. extract the form inputs (at least one of which is invalid in this error case), and
88
-
3. 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).
89
-
90
-
Solving these challenges is possible, but gets high-complexity pretty quickly.
91
-
92
-
### Approaches
93
-
94
-
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.
95
-
96
-
Here are some patterns we've considered for server-side error handling:
97
-
98
-
<style>
99
-
.styled-table, .styled-tableth, .styled-tabletd {
100
-
border: 1pxsolidblack;
101
-
padding: 8px;
102
-
border-collapse: collapse;
103
-
}
104
-
105
-
.styled-tableth:nth-child(1) { width: 50%; }
106
-
.styled-tableth:nth-child(2),
107
-
.styled-tableth:nth-child(3),
108
-
.styled-tableth:nth-child(4) { width: 15%; }
109
-
.styled-tableth:nth-child(5) { width: 10%; }
110
-
</style>
111
-
112
-
<tableclass="styled-table">
113
-
<thead>
114
-
<tr>
115
-
<th>Approach</th>
116
-
<th>Returns to same page</th>
117
-
<th>Preserves form inputs</th>
118
-
<th>Follows PRG pattern</th>
119
-
<th>Complexity</th>
120
-
</tr>
121
-
</thead>
122
-
<tbody>
123
-
<tr>
124
-
<td>Validate with Pydantic dependency, catch and redirect from middleware (with exception message as context) to an error page with "go back" button</td>
125
-
<td>No</td>
126
-
<td>Yes</td>
127
-
<td>Yes</td>
128
-
<td>Low</td>
129
-
</tr>
130
-
<tr>
131
-
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param</td>
132
-
<td>Yes</td>
133
-
<td>No</td>
134
-
<td>Yes</td>
135
-
<td>Medium</td>
136
-
</tr>
137
-
<tr>
138
-
<td>Validate in FastAPI endpoint function body, redirect to origin page with error message query param and form inputs as context so we can re-render page with original form inputs</td>
139
-
<td>Yes</td>
140
-
<td>Yes</td>
141
-
<td>Yes</td>
142
-
<td>High</td>
143
-
</tr>
144
-
<tr>
145
-
<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>
146
-
<td>Yes</td>
147
-
<td>Yes</td>
148
-
<td>Yes</td>
149
-
<td>High</td>
150
-
</tr>
151
-
<tr>
152
-
<td>Validate in either Pydantic dependency or function endpoint body and directly return error message or error toast HTML partial in JSON, then mount error toast with HTMX or some simple layout-level Javascript</td>
153
-
<td>Yes</td>
154
-
<td>Yes</td>
155
-
<td>No</td>
156
-
<td>Low</td>
157
-
</tr>
158
-
</tbody>
159
-
</table>
160
-
161
-
Presently this template primarily uses option 1 but also supports option 2. Ultimately, I think option 5 will be preferable; support for that [is planned](https://github.com/Promptly-Technologies-LLC/fastapi-jinja2-postgres-webapp/issues/5) for a future update or fork of this template.
64
+
A future iteration of this application will use HTMX to update the page in place, so that on an invalid submission an error toast is displayed without a page reload (thus preserving the user's scroll position and form inputs).
Copy file name to clipboardExpand all lines: docs/contributing.qmd
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -43,13 +43,17 @@ To contribute code to the project:
43
43
44
44
### Rendering the documentation
45
45
46
-
The README and documentation website are rendered with [Quarto](https://quarto.org/docs/). If you ,make changes to the `.qmd` files in the root folder and the `docs` folder, run the following commands to re-render the docs:
46
+
The README and documentation website are rendered with [Quarto](https://quarto.org/docs/). If you make changes to the `.qmd` files in the root folder and the `docs` folder, you will need to re-render the docs with Quarto.
47
+
48
+
Quarto expects environment variables to be set in a file called `_environment`, so before running Quarto render commands, you should copy your `.env` file to `_environment`.
47
49
48
50
```bash
51
+
# To copy the .env file to _environment
52
+
cp .env _environment
49
53
# To render the documentation website
50
-
quarto render
54
+
uv run quarto render
51
55
# To render the README
52
-
quarto render index.qmd --output-dir . --output README.md --to gfm
56
+
uv run quarto render index.qmd --output-dir . --output README.md --to gfm
53
57
```
54
58
55
59
Due to a quirk of Quarto, an unnecessary `index.html` file is created in the root folder when the README is rendered. This file can be safely deleted.
@@ -74,5 +78,5 @@ When creating new features,
74
78
To publish the documentation to GitHub Pages, run the following command:
0 commit comments