reflex-django 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reflex_django-0.1.0/.gitignore +14 -0
- reflex_django-0.1.0/CHANGELOG.md +27 -0
- reflex_django-0.1.0/LICENSE +201 -0
- reflex_django-0.1.0/PKG-INFO +346 -0
- reflex_django-0.1.0/README.md +311 -0
- reflex_django-0.1.0/RELEASING.md +71 -0
- reflex_django-0.1.0/pyproject.toml +80 -0
- reflex_django-0.1.0/python/reflex_django_reflex_cli.pth +1 -0
- reflex_django-0.1.0/reflex_django_tests/__init__.py +1 -0
- reflex_django-0.1.0/reflex_django_tests/conftest.py +16 -0
- reflex_django-0.1.0/reflex_django_tests/django_settings.py +62 -0
- reflex_django-0.1.0/reflex_django_tests/test_asgi_build.py +93 -0
- reflex_django-0.1.0/reflex_django_tests/test_auth_state.py +55 -0
- reflex_django-0.1.0/reflex_django_tests/test_authz.py +66 -0
- reflex_django-0.1.0/reflex_django_tests/test_cli.py +102 -0
- reflex_django-0.1.0/reflex_django_tests/test_conf.py +80 -0
- reflex_django-0.1.0/reflex_django_tests/test_dispatcher.py +155 -0
- reflex_django-0.1.0/reflex_django_tests/test_event_bridge.py +201 -0
- reflex_django-0.1.0/reflex_django_tests/test_i18n_bridge.py +104 -0
- reflex_django-0.1.0/reflex_django_tests/test_init_project.py +58 -0
- reflex_django-0.1.0/reflex_django_tests/test_model.py +56 -0
- reflex_django-0.1.0/reflex_django_tests/test_plugin.py +307 -0
- reflex_django-0.1.0/reflex_django_tests/test_reflex_context.py +128 -0
- reflex_django-0.1.0/src/reflex_django/__init__.py +155 -0
- reflex_django-0.1.0/src/reflex_django/_reflex_cli_bootstrap.py +45 -0
- reflex_django-0.1.0/src/reflex_django/admin.py +57 -0
- reflex_django-0.1.0/src/reflex_django/apps.py +20 -0
- reflex_django-0.1.0/src/reflex_django/asgi.py +174 -0
- reflex_django-0.1.0/src/reflex_django/auth_state.py +140 -0
- reflex_django-0.1.0/src/reflex_django/authz.py +104 -0
- reflex_django-0.1.0/src/reflex_django/cli.py +166 -0
- reflex_django-0.1.0/src/reflex_django/conf.py +91 -0
- reflex_django-0.1.0/src/reflex_django/context.py +160 -0
- reflex_django-0.1.0/src/reflex_django/default_settings.py +188 -0
- reflex_django-0.1.0/src/reflex_django/i18n_state.py +40 -0
- reflex_django-0.1.0/src/reflex_django/init_project.py +474 -0
- reflex_django-0.1.0/src/reflex_django/middleware.py +225 -0
- reflex_django-0.1.0/src/reflex_django/model.py +80 -0
- reflex_django-0.1.0/src/reflex_django/plugin.py +184 -0
- reflex_django-0.1.0/src/reflex_django/py.typed +0 -0
- reflex_django-0.1.0/src/reflex_django/reflex_context.py +289 -0
- reflex_django-0.1.0/src/reflex_django/session_js.py +61 -0
- reflex_django-0.1.0/src/reflex_django/urls.py +26 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Changed (breaking)
|
|
11
|
+
|
|
12
|
+
- `DjangoUserState` snapshot field names no longer use a `django_` prefix:
|
|
13
|
+
`django_user_id` → `user_id`, `django_username` → `username`,
|
|
14
|
+
`django_email` → `email`, `django_first_name` → `first_name`,
|
|
15
|
+
`django_last_name` → `last_name`, `django_is_authenticated` → `is_authenticated`,
|
|
16
|
+
`django_is_staff` → `is_staff`, `django_is_superuser` → `is_superuser`,
|
|
17
|
+
`django_group_names` → `group_names`.
|
|
18
|
+
|
|
19
|
+
## [0.1.0] — 2026-05-14
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- Initial PyPI-ready release of `reflex-django`: Django ASGI integration with
|
|
24
|
+
Reflex (`ReflexDjangoPlugin`), `DjangoEventBridge`, auth/session/locale
|
|
25
|
+
bridging, context processors, helpers, and CLI entry point `reflex-django`.
|
|
26
|
+
|
|
27
|
+
[0.1.0]: https://pypi.org/project/reflex-django/
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023, Pynecone, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reflex-django
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Run a Django backend (Django ORM, Django Admin) alongside a Reflex app.
|
|
5
|
+
Project-URL: homepage, https://reflex.dev
|
|
6
|
+
Project-URL: documentation, https://github.com/reflex-dev/reflex/tree/main/packages/reflex-django
|
|
7
|
+
Project-URL: repository, https://github.com/reflex-dev/reflex
|
|
8
|
+
Project-URL: changelog, https://github.com/reflex-dev/reflex/releases
|
|
9
|
+
Author: Reflex Contributors
|
|
10
|
+
Maintainer: Reflex Contributors
|
|
11
|
+
Maintainer-email: maintainers@reflex.dev
|
|
12
|
+
License-Expression: Apache-2.0
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Keywords: admin,asgi,django,fullstack,orm,reflex
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Environment :: Web Environment
|
|
17
|
+
Classifier: Framework :: Django
|
|
18
|
+
Classifier: Framework :: Django :: 6.0
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
26
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: <4.0,>=3.12
|
|
30
|
+
Requires-Dist: asgiref>=3.8
|
|
31
|
+
Requires-Dist: click>=8.0
|
|
32
|
+
Requires-Dist: django<7.0,>=6.0
|
|
33
|
+
Requires-Dist: reflex<1.0,>=0.9.2
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# reflex-django
|
|
37
|
+
|
|
38
|
+
## Table of contents
|
|
39
|
+
|
|
40
|
+
1. [About reflex-django](#about-reflex-django)
|
|
41
|
+
2. [Architecture](#architecture)
|
|
42
|
+
3. [How to set up](#how-to-set-up)
|
|
43
|
+
4. [Commands](#commands)
|
|
44
|
+
5. [States, context, and bridges](#states-context-and-bridges)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## About reflex-django
|
|
49
|
+
|
|
50
|
+
**reflex-django** is a [Reflex](https://reflex.dev) plugin that runs a **Django ASGI** application and your **Reflex** app in **one process** under a single dev command (`reflex run`). HTTP requests whose paths match configured prefixes (for example Django Admin, optional API routes, and path-based static URLs) are forwarded to Django. Everything else—including the compiled Reflex frontend and Reflex’s Socket.IO event channel under `/_event/…`—is handled by Reflex.
|
|
51
|
+
|
|
52
|
+
**Why it exists.** Reflex gives you a Python-first reactive UI. Django gives you the ORM, migrations, the admin, sessions, authentication, internationalization, and the ecosystem of HTTP views and middleware you already rely on. reflex-django lets you keep that Django surface area without standing up a separate HTTP server for local development or simple deployments, while still using Reflex for the interactive UI.
|
|
53
|
+
|
|
54
|
+
**Why Django developers need it.** Reflex user actions arrive over WebSocket events, not through Django’s normal request/response cycle, so Django’s HTTP middleware (sessions, auth, locale) does not run for those events by default. reflex-django adds an explicit **event bridge** that rebuilds a synthetic `HttpRequest` from the browser data Reflex provides, attaches the session and user, and exposes that request through small APIs your event handlers can call. A separate **HTTP bridge** routes ordinary browser HTTP traffic on selected path prefixes to Django’s ASGI app. Together, these bridges make Django session auth and related patterns usable from Reflex without pretending the two stacks share one router.
|
|
55
|
+
|
|
56
|
+
**Author.** This package is written by **Mohannad Irshedat**.
|
|
57
|
+
|
|
58
|
+
| Topic | Versions |
|
|
59
|
+
|---------|----------|
|
|
60
|
+
| Django | 6.0.x |
|
|
61
|
+
| Python | 3.12+ |
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Architecture
|
|
66
|
+
|
|
67
|
+
At a high level, reflex-django does three coordinated things:
|
|
68
|
+
|
|
69
|
+
1. **Plugin bootstrap** — When `rxconfig.py` is loaded, `ReflexDjangoPlugin` sets `DJANGO_SETTINGS_MODULE` (if you pass `settings_module`), exports path prefix environment variables used by Django settings, and calls `configure_django()` so Django is initialized before your Reflex app module imports models or translation helpers.
|
|
70
|
+
|
|
71
|
+
2. **HTTP ASGI composition** — After Reflex compiles your app, the plugin appends an `api_transformer` that wraps Reflex’s inner ASGI app. Incoming HTTP (and WebSocket upgrade traffic where applicable) is dispatched by **URL path prefix**: matching prefixes go to Django’s ASGI application (optionally wrapped for static files); non-matching traffic stays on Reflex. ASGI lifespan events are owned by Reflex.
|
|
72
|
+
|
|
73
|
+
3. **Per-event Django context** — When `install_event_bridge` is true (the default), Reflex registers `DjangoEventBridge` middleware. On each Reflex event, the bridge builds a synthetic `django.http.HttpRequest` from `event.router_data` (cookies, headers, client IP, path), loads the Django session, optionally applies the same locale negotiation as `LocaleMiddleware`, resolves `request.user` via Django’s async `aget_user`, and binds that request on a **context variable** for the duration of the event. Your handlers call `current_user()`, `current_request()`, and related helpers, which read that binding.
|
|
74
|
+
|
|
75
|
+
.
|
|
76
|
+
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## How to set up
|
|
84
|
+
|
|
85
|
+
Use **uv** to create a project, add dependencies, scaffold the Reflex frontend, create a Django project, then wire the plugin in `rxconfig.py`.
|
|
86
|
+
|
|
87
|
+
1. Initialize a Python project:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
uv init
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
2. Add Reflex and reflex-django:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
uv add reflex reflex-django
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
3. Initialize the Reflex frontend (app name and layout follow Reflex’s CLI):
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
uv run reflex init frontend
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
4. Create a Django project package named `backend` (adjust the name if you prefer):
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
uv run django-admin startproject backend .
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
This typically produces `backend/settings.py`, `backend/urls.py`, and `manage.py` in the current directory.
|
|
112
|
+
|
|
113
|
+
5. Configure **`rxconfig.py`** so Reflex loads Django with your settings module. Import the plugin and pass `settings_module` as the dotted path to your Django settings (for example `backend.settings`):
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import reflex as rx
|
|
117
|
+
from reflex_django import ReflexDjangoPlugin
|
|
118
|
+
|
|
119
|
+
config = rx.Config(
|
|
120
|
+
app_name="myapp",
|
|
121
|
+
plugins=[
|
|
122
|
+
ReflexDjangoPlugin(settings_module="backend.settings"),
|
|
123
|
+
],
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Minimal Django expectations.** Your `INSTALLED_APPS` should include the Django contrib apps you need (for example `django.contrib.auth`, `django.contrib.sessions`, and often `django.contrib.admin`) and **`reflex_django`** if you use bundled helpers. Set `ROOT_URLCONF` and mount admin (and any HTTP routes under an optional `backend_prefix`) so path-based routing matches what you configure on `ReflexDjangoPlugin`. Run the app with:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
uv run reflex run
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Commands
|
|
136
|
+
|
|
137
|
+
reflex-django registers a **`django`** command group on the **`reflex`** CLI (via a small import hook installed with the package) and also ships a standalone console script **`reflex-django`**.
|
|
138
|
+
|
|
139
|
+
**Running Django management commands**
|
|
140
|
+
|
|
141
|
+
- Through Reflex:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uv run reflex django migrate
|
|
145
|
+
uv run reflex django makemigrations
|
|
146
|
+
uv run reflex django createsuperuser
|
|
147
|
+
uv run reflex django shell
|
|
148
|
+
uv run reflex django collectstatic
|
|
149
|
+
uv run reflex django help
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- Through the standalone entry point (equivalent forwarding for normal manage.py subcommands):
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
uv run reflex-django migrate
|
|
156
|
+
uv run reflex-django help
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Any subcommand name other than the special cases below is forwarded to Django’s `execute_from_command_line`. The wrapper first loads your **`rxconfig`** (so `ReflexDjangoPlugin` can set `DJANGO_SETTINGS_MODULE` and path prefixes) and then calls **`configure_django()`**, so the same settings module Reflex uses at runtime is used for migrations and other management commands.
|
|
160
|
+
|
|
161
|
+
**Init scaffolding (omitted here).** `reflex django init` and `reflex-django init` exist to scaffold a starter tree but are considered **beta**; this README does not document them. Prefer the manual flow in [How to set up](#how-to-set-up) above.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## States, context, and bridges
|
|
166
|
+
|
|
167
|
+
This section walks through Reflex state, Django’s per-event request context (context variables), the two bridges, and the helper states that mirror Django into Reflex UI state—each with a small example.
|
|
168
|
+
|
|
169
|
+
### Reflex `rx.State` (baseline)
|
|
170
|
+
|
|
171
|
+
In Reflex, a **State** class subclasses `rx.State`. Fields you declare are synchronized with the client where appropriate; values must be **JSON-serializable** when they cross the wire. You define event handlers with `@rx.event` (often `async def` when you call async Django APIs).
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
import reflex as rx
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class CounterState(rx.State):
|
|
178
|
+
count: int = 0
|
|
179
|
+
|
|
180
|
+
@rx.event
|
|
181
|
+
def increment(self):
|
|
182
|
+
self.count += 1
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Per-event Django context (`reflex_django.context`)
|
|
186
|
+
|
|
187
|
+
Reflex events do not carry a Django `HttpRequest` object. reflex-django stores the synthetic request built for the current event on a **context variable**. Public read helpers:
|
|
188
|
+
|
|
189
|
+
| Function | Role |
|
|
190
|
+
|----------|------|
|
|
191
|
+
| `current_request()` | Bound `HttpRequest` or `None` outside an event without the bridge |
|
|
192
|
+
| `current_user()` | Django user or `AnonymousUser` |
|
|
193
|
+
| `current_session()` | Session backend instance or `None` |
|
|
194
|
+
| `current_language()` | Active language code after locale activation |
|
|
195
|
+
|
|
196
|
+
Lower-level **`begin_event_request(request)`** and **`end_event_request()`** are used by the bridge and for tests or advanced scenarios; application code normally relies on the helpers above inside `@rx.event` handlers.
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
import reflex as rx
|
|
200
|
+
from reflex_django import current_user
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class WhoamiState(rx.State):
|
|
204
|
+
label: str = ""
|
|
205
|
+
|
|
206
|
+
@rx.event
|
|
207
|
+
async def refresh(self):
|
|
208
|
+
user = current_user()
|
|
209
|
+
self.label = user.get_username() if user.is_authenticated else "anonymous"
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Bridge 1 — HTTP ASGI path dispatcher
|
|
213
|
+
|
|
214
|
+
`ReflexDjangoPlugin` installs an **`api_transformer`** that wraps Reflex’s ASGI app. Requests whose path starts with any configured prefix are sent to Django’s ASGI app; other paths stay on Reflex. Relevant plugin arguments:
|
|
215
|
+
|
|
216
|
+
| Argument | Meaning |
|
|
217
|
+
|----------|---------|
|
|
218
|
+
| `backend_prefix` | Optional prefix for your own Django HTTP routes (for example `"/api"`) |
|
|
219
|
+
| `admin_prefix` | Prefix for Django Admin (default `"/admin"`) |
|
|
220
|
+
| `extra_prefixes` | Additional path prefixes routed to Django |
|
|
221
|
+
| `install_event_bridge` | When `True`, registers `DjangoEventBridge` (default) |
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
ReflexDjangoPlugin(
|
|
225
|
+
settings_module="backend.settings",
|
|
226
|
+
backend_prefix="/api",
|
|
227
|
+
admin_prefix="/admin",
|
|
228
|
+
extra_prefixes=("/billing",),
|
|
229
|
+
)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Your `ROOT_URLCONF` must define routes under those prefixes so Django can serve them.
|
|
233
|
+
|
|
234
|
+
### Bridge 2 — `DjangoEventBridge` (Reflex middleware)
|
|
235
|
+
|
|
236
|
+
**`DjangoEventBridge`** runs at the start of each Reflex event. It constructs a synthetic `HttpRequest`, attaches the session, optionally runs locale negotiation when `USE_I18N` and `REFLEX_DJANGO_I18N_EVENT_BRIDGE` allow it, and sets `request.user` using **`aget_user`**. It then calls `begin_event_request(request)` so `current_user()` and friends work in your handlers.
|
|
237
|
+
|
|
238
|
+
To disable this behavior (for example if you do not use Django session auth from Reflex):
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
ReflexDjangoPlugin(settings_module="backend.settings", install_event_bridge=False)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### `DjangoUserState`
|
|
245
|
+
|
|
246
|
+
**`DjangoUserState`** is a `rx.State` subclass that mirrors a JSON-safe snapshot of the current user (`user_id`, `username`, `email`, names, `is_authenticated`, staff/superuser flags, optional `group_names`). Use it for navbar and conditional UI.
|
|
247
|
+
|
|
248
|
+
- Call **`sync_from_django`** from a page’s **`on_load`** (it is an `@rx.event`).
|
|
249
|
+
- After login or logout, call **`await self.refresh_django_user_fields()`** from an async handler so the snapshot updates without a full navigation when appropriate.
|
|
250
|
+
|
|
251
|
+
Server-side authorization must still use **`current_user()`** (or stricter checks); client-visible state is for display only.
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
import reflex as rx
|
|
255
|
+
from reflex_django import DjangoUserState
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class AuthState(DjangoUserState):
|
|
259
|
+
pass
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
app = rx.App()
|
|
263
|
+
|
|
264
|
+
# app.add_page(index, on_load=AuthState.sync_from_django)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### `DjangoI18nState`
|
|
268
|
+
|
|
269
|
+
**`DjangoI18nState`** exposes **`django_language_code`** and **`django_language_bidi`**, aligned with Django’s active language after the event bridge. Use **`sync_from_django`** on `on_load`, or **`await self.refresh_django_i18n_fields()`** after the user changes language via a Django-served view.
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
import reflex as rx
|
|
273
|
+
from reflex_django import DjangoI18nState
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
app = rx.App()
|
|
277
|
+
|
|
278
|
+
# app.add_page(home, on_load=DjangoI18nState.sync_from_django)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Reflex-facing Django context (processors and `DjangoContextState`)
|
|
282
|
+
|
|
283
|
+
Reflex serializes state to the browser, so any dict you merge into Reflex state must be **JSON-serializable**.
|
|
284
|
+
|
|
285
|
+
**Two configuration modes** (see `reflex_django.reflex_context`):
|
|
286
|
+
|
|
287
|
+
1. **`REFLEX_DJANGO_CONTEXT_PROCESSORS`** — A non-empty tuple of dotted paths to callables `(request) -> dict` (or async). When this list is set, it is used exclusively. You are responsible for JSON-safe values.
|
|
288
|
+
|
|
289
|
+
2. **Template context processors** — If `REFLEX_DJANGO_CONTEXT_PROCESSORS` is empty and **`REFLEX_DJANGO_USE_TEMPLATE_CONTEXT_PROCESSORS`** is `True`, the same dotted paths as in `TEMPLATES[*].OPTIONS["context_processors"]` are run, with built-in sanitization (for example `user` becomes a snapshot; `request`, `perms`, `messages` are omitted; other values must pass plain `json.dumps`).
|
|
290
|
+
|
|
291
|
+
Built-in processor callables you can list explicitly:
|
|
292
|
+
|
|
293
|
+
- **`reflex_django.reflex_context.builtin_user_context`** — adds a template-shaped `user` key as a snapshot dict.
|
|
294
|
+
- **`reflex_django.reflex_context.builtin_i18n_context`** — adds `LANGUAGE_CODE`, `LANGUAGE_BIDI`, and `LANGUAGES`.
|
|
295
|
+
|
|
296
|
+
Example settings snippet:
|
|
297
|
+
|
|
298
|
+
```python
|
|
299
|
+
REFLEX_DJANGO_CONTEXT_PROCESSORS = (
|
|
300
|
+
"reflex_django.reflex_context.builtin_user_context",
|
|
301
|
+
"reflex_django.reflex_context.builtin_i18n_context",
|
|
302
|
+
)
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
**`collect_reflex_context(request)`** runs the configured processor list and returns one merged dict. You can await it inside a handler when `current_request()` is bound:
|
|
306
|
+
|
|
307
|
+
```python
|
|
308
|
+
from reflex_django import current_request
|
|
309
|
+
from reflex_django.reflex_context import collect_reflex_context
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
@rx.event
|
|
313
|
+
async def debug_context(self):
|
|
314
|
+
merged = await collect_reflex_context(current_request())
|
|
315
|
+
# use merged (e.g. assign JSON-safe keys to state)
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
**`DjangoContextState`** holds **`django_context`** and a stringified **`django_context_json`**. Call **`load_django_context`** from `on_load` to populate them from processors using the bound request:
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
from reflex_django import DjangoContextState
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
# app.add_page(about, on_load=DjangoContextState.load_django_context)
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Helper functions **`template_context_processor_paths()`** and **`reflex_context_processor_paths()`** introspect settings for tooling or documentation.
|
|
328
|
+
|
|
329
|
+
### `user_snapshot` vs `current_user`
|
|
330
|
+
|
|
331
|
+
**`user_snapshot(user)`** returns the same flat dict shape used by `DjangoUserState`, given a user instance. It is useful in custom context processors, tests, or logging—without touching Reflex state.
|
|
332
|
+
|
|
333
|
+
**`current_user()`** returns the live Django user (or anonymous) for the **current Reflex event** after the bridge runs. Use it for **permissions, ownership checks, and mutations** on the server.
|
|
334
|
+
|
|
335
|
+
**`DjangoUserState`** fields are a **UI snapshot**; always re-check authorization with `current_user()` (or equivalent) inside event handlers that change data.
|
|
336
|
+
|
|
337
|
+
```python
|
|
338
|
+
from reflex_django import current_user
|
|
339
|
+
from reflex_django.auth_state import user_snapshot
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
@rx.event
|
|
343
|
+
async def audit_banner(self):
|
|
344
|
+
self.snapshot_json = user_snapshot(current_user()) # dict for display
|
|
345
|
+
assert current_user().is_authenticated # real check for protected actions
|
|
346
|
+
```
|