Skip to content

Commit 8ec7369

Browse files
30 - Form, State, and Page for New Blog Posts
1 parent 325b1ef commit 8ec7369

File tree

9 files changed

+99
-2
lines changed

9 files changed

+99
-2
lines changed

full_stack_python/blog/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from .add import blog_post_add_page
12
from .list import blog_post_list_page
23
from .model import BlogPostModel
34
from .state import BlogPostState
45

56
__all__= [
7+
'blog_post_add_page',
68
'blog_post_list_page',
79
'BlogPostModel',
810
'BlogPostState'

full_stack_python/blog/add.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import reflex as rx
2+
3+
from ..ui.base import base_page
4+
from . import forms
5+
6+
def blog_post_add_page() -> rx.Component:
7+
my_form = forms.blog_post_add_form()
8+
my_child = rx.vstack(
9+
rx.heading("New Blog Post", size="9"),
10+
rx.desktop_only(
11+
rx.box(
12+
my_form,
13+
width='50vw'
14+
)
15+
),
16+
rx.tablet_only(
17+
rx.box(
18+
my_form,
19+
width='75vw'
20+
)
21+
),
22+
rx.mobile_only(
23+
rx.box(
24+
my_form,
25+
width='95vw'
26+
)
27+
),
28+
spacing="5",
29+
align="center",
30+
min_height="95vh",
31+
)
32+
return base_page(my_child)

full_stack_python/blog/forms.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import reflex as rx
2+
3+
4+
from . import state
5+
6+
def blog_post_add_form() -> rx.Component:
7+
return rx.form(
8+
rx.vstack(
9+
rx.hstack(
10+
rx.input(
11+
name="title",
12+
placeholder="Title",
13+
required=True,
14+
type='text',
15+
width='100%',
16+
),
17+
width='100%'
18+
),
19+
rx.text_area(
20+
name='content',
21+
placeholder="Your message",
22+
required=True,
23+
height='50vh',
24+
width='100%',
25+
),
26+
rx.button("Submit", type="submit"),
27+
),
28+
on_submit=state.BlogAddPostFormState.handle_submit,
29+
reset_on_submit=True,
30+
)

full_stack_python/blog/list.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def blog_post_list_item(post: model.BlogPostModel):
3333
def blog_post_list_page() ->rx.Component:
3434
return base_page(
3535
rx.vstack(
36-
rx.heading("Blog Posts", size="5"),
36+
rx.heading("Blog Posts", size="5"),
37+
rx.link(
38+
rx.button("New Post"),
39+
href=navigation.routes.BLOG_POST_ADD_ROUTE
40+
),
3741
# rx.foreach(["abc", "abc", "cde"], foreach_callback),
3842
rx.foreach(state.BlogPostState.posts, blog_post_list_item),
3943
spacing="5",

full_stack_python/blog/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class BlogPostModel(rx.Model, table=True):
1010
# user
11+
# id: int -> primary key
1112
title: str
1213
content: str
1314
created_at: datetime = Field(

full_stack_python/blog/state.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,27 @@ def load_posts(self):
1717
self.posts = result
1818
# return
1919

20+
def add_post(self, form_data:dict):
21+
with rx.session() as session:
22+
post = BlogPostModel(**form_data)
23+
# print("adding", post)
24+
session.add(post)
25+
session.commit()
26+
session.refresh(post) # post.id
27+
# print("added", post)
28+
self.post = post
2029
# def get_post(self):
2130
# with rx.session() as session:
2231
# result = session.exec(
2332
# select(BlogPostModel)
2433
# )
25-
# self.posts = result
34+
# self.posts = result
35+
36+
37+
class BlogAddPostFormState(BlogPostState):
38+
form_data: dict = {}
39+
40+
def handle_submit(self, form_data):
41+
self.form_data = form_data
42+
self.add_post(form_data)
43+
# redirect

full_stack_python/full_stack_python.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def index() -> rx.Component:
5656

5757
)
5858

59+
app.add_page(
60+
blog.blog_post_add_page,
61+
route=navigation.routes.BLOG_POST_ADD_ROUTE
62+
)
63+
5964
app.add_page(contact.contact_page,
6065
route=navigation.routes.CONTACT_US_ROUTE)
6166
app.add_page(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
HOME_ROUTE="/"
22
ABOUT_US_ROUTE="/about"
33
BLOG_POSTS_ROUTE="/blog"
4+
BLOG_POST_ADD_ROUTE="/blog/add"
45
CONTACT_US_ROUTE="/contact"
56
CONTACT_ENTRIES_ROUTE="/contact/entries"
67
PRICING_ROUTE="/pricing"

full_stack_python/navigation/state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def to_about_us(self):
99
return rx.redirect(routes.ABOUT_US_ROUTE)
1010
def to_blog(self):
1111
return rx.redirect(routes.BLOG_POSTS_ROUTE)
12+
def to_blog_add(self):
13+
return rx.redirect(routes.BLOG_POST_ADD_ROUTE)
14+
def to_blog_create(self):
15+
return self.to_blog_add()
1216
def to_contact(self):
1317
return rx.redirect(routes.CONTACT_US_ROUTE)
1418
def to_pricing(self):

0 commit comments

Comments
 (0)