Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We don't want to have to type out all of the template code required to render forms every single time we render a form. Let's make a Jinja2 macro, or a template function, that'll handle this work for us.
New terms
-
{% macro %}
- A function in a template to repeat code on demand. Often really useful for things like form fields.
Note
If you're constantly getting a locked database, change your User.create_user
method to the following:
@classmethod
def create_user(cls, username, email, password, admin=False):
try:
with DATABASE.transaction():
cls.create(
username=username,
email=email,
password=generate_password_hash(password),
is_admin=admin)
except IntegrityError:
raise ValueError("User already exists")
What I've done here is rendered out our
registration.
0:00
We're gonna be doing this a lot.
0:04
So, we probably wanna make a way to not
have to do that a lot cuz we're lazy.
0:08
Right?
We're programmers.
0:13
So, Gender2 has this handy thing called
Macros and we can build Macros.
0:15
So, let's make a macro that'll render a
field for us.
0:22
So, new file, I'm gonna call this
macros.html and sorry,
0:25
I gotta put it into the right folder
there.
0:30
Okay.
0:34
And inside macros.html, we just write our
macros as HTML.
0:35
So, I'm gonna take this whole thing.
0:39
And paste it in here.
0:43
And my spacing is a little weird.
0:45
Let's just select all of that.
0:46
Have that back out one and
0:50
then what we need to do is here we need to
do macro render_field.
0:51
That is the name of our macros
render_field.
0:56
And then we're going to pass it a field.
0:58
And then, down here at the bottom, we're
going to endmacro.
1:00
So we took all of our code, and let's take
it out of here.
1:05
We don't need that anymore.
1:08
It's in macros.
1:10
So now inside here, we want to call, or we
want to run printout.
1:11
Render_field, and then the field that
we're on.
1:16
And to do that, just like we have to
import functions and stuff, and
1:21
we need to do from macros.html import
render_field.
1:25
All right, let's see if this works.
1:31
So we come down here.
1:35
And we do python app.py.
1:37
We have an error on line 4.
1:40
We forgot our comma after email.
1:45
All right.
1:47
And we have a problem down here.
1:50
This should be username.
1:52
[BLANK_AUDIO]
1:53
Okay, so we need to put a try around this.
2:02
And if we get back our value error, then
we'll just pass on this one.
2:11
Because it's okay.
2:15
The error comes out and we're like, yep.
2:17
User's already there, that's fine.
2:20
All right, there we go.
2:23
So there's our app running.
2:23
And let's preview it.
2:27
[SOUND]
Okay.
2:28
Sorry.
We had some issues here with running this.
2:33
So I actually ran this and I kept getting
an error about the database being locked.
2:36
If that comes up, delete your database and
restart python app.py.
2:40
So, anyway, and now that it's running,
2:45
we're gonna go up here to our little I
icon and we're gonna click port 8000, and
2:47
we've got hey, of course, cuz we went to
the home page.
2:52
Let's go to register.
2:55
And look at that.
2:57
There is our form.
2:58
Here's each of our fields.
2:59
And something that we forgot, let's go add
that.
3:00
We'll go back to our register.html.
3:03
We need to add an input or a submit.
3:07
So, we're actually gonna do a button.
3:10
I go submit ID equals submit.
3:11
And we're gonna say register.
3:13
All right.
3:16
And let's go refresh this.
3:16
There's our Register button.
3:19
And if I click that, I get the fields are
required.
3:20
So let's do a user name that's wrong.
3:24
And let's hit Register and I get that it's
not right, right?
3:27
It should be numbers, letters, underscore,
etc.
3:32
Let's do that right.
3:35
Now let's do something that's not an email
address.
3:36
Mm, invalid email address, okay.
3:40
So, hello@example.com.
3:46
All right, this field's required.
3:50
Let's just do P for my password.
3:52
Oh, it must be at least two characters
long and passwords must match,
3:55
because this password doesn't match that
password.
3:59
So, you can see that our validation is
working like we're expecting.
4:01
Now, that's great.
4:04
Macros are one of the most useful parts of
Jinja2 which is our template engine and
4:07
obviously, being able to show users errors
makes it a lot easier to sign up.
4:11
If you give a user an account, they're
gonna want to sign in.
4:15
Let's do that now.
4:18
You need to sign up for Treehouse in order to download course files.
Sign up