Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Flask with SQLAlchemy Basics!
      
    
You have completed Flask with SQLAlchemy Basics!
Preview
    
      
  Use flask-sqlalchemy to create routes for reading, updating, and deleting entries in your pet database.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      We have a working form and
a functional home page.
                      0:00
                    
                    
                      The last page, our pet page needs to be
updated to use Flask templating as well.
                      0:04
                    
                    
                      When we click on a card,
we're taken to the placeholder
                      0:10
                    
                    
                      information instead of
that pet's information.
                      0:14
                    
                    
                      Let's start inside index.html.
                      0:18
                    
                    
                      Each card is wrapped
inside of an anchor tag.
                      0:25
                    
                    
                      Let's update this link to
include the pet's unique id.
                      0:29
                    
                    
                      So here we can do, id=pet.id.
                      0:35
                    
                    
                      This will request our pet page and
send it the pets unique id number so
                      0:40
                    
                    
                      that we can find the right pet to display.
                      0:45
                    
                    
                      Now let's jump over into app.py.
                      0:49
                    
                    
                      Let's go to our pet page, there we go.
                      0:55
                    
                    
                      And we need to update the route to accept
our ID variable, we need to add / and
                      0:58
                    
                    
                      then we're gonna need the carets
here on either side with id.
                      1:04
                    
                    
                      This is how you can pass information
from one page to another.
                      1:10
                    
                    
                      The function will also
need to take an argument,
                      1:15
                    
                    
                      id which will be the id being
passed in to our function.
                      1:18
                    
                    
                      Let's save the file and
let's see this in action.
                      1:24
                    
                    
                      If I click on Lucy,
you can see I get pet and
                      1:31
                    
                    
                      then I get 1, cuz Lucy's id is 1.
                      1:35
                    
                    
                      If I click on Blanca, I get 2, perfect.
                      1:39
                    
                    
                      We can see our id is
getting passed to the page.
                      1:42
                    
                    
                      One part is done.
                      1:47
                    
                    
                      We passed a pet's unique
id to the pet page.
                      1:49
                    
                    
                      Now we need to use that id to find
our pet and send it to our template.
                      1:52
                    
                    
                      Kind of like what we did on the homepage.
                      1:58
                    
                    
                      Let's create a new variable called pet =,
and
                      2:00
                    
                    
                      let's figure out how we can
query our database by id.
                      2:05
                    
                    
                      Now it's gonna be similar to
how we've queried before.
                      2:11
                    
                    
                      So pet.query, so
we can start with that, pet.query.
                      2:15
                    
                    
                      And let's check our docks,
looks like we can use a filter.
                      2:22
                    
                    
                      We can order by, or
we can get user by primary key,
                      2:28
                    
                    
                      which our id is our primary id, so
we can do get and then pass in that id.
                      2:33
                    
                    
                      .get, let's pass in id.
                      2:44
                    
                    
                      And now we can pass that into
our template, pet = pet.
                      2:50
                    
                    
                      Now we can jump over to our pet page.
                      2:57
                    
                    
                      pet.html, and we can switch out our
information here for our pets information.
                      3:01
                    
                    
                      Looks like I'm towards the bottom,
so let's go ahead and
                      3:07
                    
                    
                      switch this out for pet.description.
                      3:14
                    
                    
                      And then can change out
all of their values.
                      3:21
                    
                    
                      Don't forget to change out the URL for
the image as well.
                      3:46
                    
                    
                      Tag, there we go.
                      3:57
                    
                    
                      And let's try this in the browser.
                      3:59
                    
                    
                      Now if I refresh, perfect.
                      4:04
                    
                    
                      I see Blanca and
all of Blanca's information, amazing.
                      4:06
                    
                    
                      Let's test it back on the homepage just
to make sure, let's try Bridgette,
                      4:14
                    
                    
                      And Bubblegum, awesome.
                      4:22
                    
                    
                      Now, what if you made a mistake
in the pet's information,
                      4:28
                    
                    
                      or what if the data needs to be updated?
                      4:31
                    
                    
                      We need to create a way to edit our pet.
                      4:34
                    
                    
                      Luckily, we already have a template page
we can use, we can use our form page.
                      4:37
                    
                    
                      Since we can pass in values, we can fill
the inputs with the pets information,
                      4:42
                    
                    
                      make our changes, and then hit submit.
                      4:47
                    
                    
                      Let's get to it.
                      4:49
                    
                    
                      First to keep things simple,
let's duplicate
                      4:52
                    
                    
                      our addpet.html file,
and to Copy and Paste.
                      4:57
                    
                    
                      And let's rename it.
                      5:06
                    
                    
                      Editpet.
                      5:10
                    
                    
                      Now in app.py, lets close out
some of these that we don't need,
                      5:12
                    
                    
                      there we go, in app.py let's
create a new route called edit.
                      5:18
                    
                    
                      And it's going to need the pet's
unique id, just like we did before.
                      5:33
                    
                    
                      So I'm gonna go ahead and
put that in here now.
                      5:38
                    
                    
                      Then let's define this as edit_pet.
                      5:42
                    
                    
                      It'll bring in an id.
                      5:45
                    
                    
                      And then we already know
how to get a pet by id,
                      5:49
                    
                    
                      so let's go ahead and
do that, Pet.query.get (id).
                      5:55
                    
                    
                      And then we can do our
return render_template
                      6:02
                    
                    
                      with editpet.html and pets=pet,
                      6:09
                    
                    
                      oops, I spell rout wrong.
                      6:14
                    
                    
                      You can see it gave me this error right
here, need an e, save, there we go.
                      6:18
                    
                    
                      Turn our website back on.
                      6:24
                    
                    
                      Okay, now inside of our pet.html,
                      6:27
                    
                    
                      I scroll down here to the bottom and
we need to set these URLs,
                      6:31
                    
                    
                      for ('edit_pet' and then id=pet.id).
                      6:38
                    
                    
                      Let's save and let's come over here
to the homepage to a hard refresh.
                      6:44
                    
                    
                      I'm just gonna use Lucy,
and when I click edit,
                      6:50
                    
                    
                      I'm taken over to the form page.
                      6:55
                    
                    
                      Now, we just need to fill the inputs
with the information we already have for
                      6:59
                    
                    
                      Lucy to make it easier on the user.
                      7:04
                    
                    
                      Now in editpet, for
                      7:07
                    
                    
                      each of our inputs we're
                      7:11
                    
                    
                      gonna come here to the end and
                      7:15
                    
                    
                      we're gonna do value=pet.,
                      7:20
                    
                    
                      and then this was name.
                      7:26
                    
                    
                      So if I hit save just show
what this looks like.
                      7:30
                    
                    
                      If I do a hard refresh, you can now
see it says Lucy inside of the input.
                      7:35
                    
                    
                      We're gonna do that for all of them.
                      7:40
                    
                    
                      Okay, in this section, we need to
select the correct radio button,
                      8:12
                    
                    
                      remember on our form, let me bring that
up so you can see what those look like.
                      8:15
                    
                    
                      We need to have the correct button
selected here automatically.
                      8:19
                    
                    
                      So, the way that looks,
                      8:24
                    
                    
                      in here after the value,
                      8:28
                    
                    
                      we're going to do an if statement,
                      8:32
                    
                    
                      so I'm gonna give it space and
                      8:38
                    
                    
                      we're gonna do % %, and
                      8:43
                    
                    
                      if pet.pet_type == 'Dog'.
                      8:47
                    
                    
                      Then we're going to add
a checked attribute and
                      8:53
                    
                    
                      that's what selects this radio button,
                      8:57
                    
                    
                      checked and then we're going to do endif.
                      9:01
                    
                    
                      And we're gonna copy that, And paste it.
                      9:05
                    
                    
                      And then this should say, 'Cat' and
                      9:14
                    
                    
                      this should say, "Other", and
                      9:19
                    
                    
                      then we can do the same thing for these.
                      9:23
                    
                    
                      So we're gonna come down
here to our inputs for
                      9:28
                    
                    
                      radio, if the gender is Female.
                      9:35
                    
                    
                      The gender is Male, save and
                      9:43
                    
                    
                      then we need to do spay.
                      9:49
                    
                    
                      If spay is 'Yes',
                      10:01
                    
                    
                      If pet.spay == 'No'.
                      10:06
                    
                    
                      And then pet.spay
                      10:11
                    
                    
                      == 'Unknown'.
                      10:16
                    
                    
                      And then finally our house_trained
                      10:21
                    
                    
                      If it's 'Yes' If it's 'No'.
                      10:33
                    
                    
                      And if it's 'Unknown'.
                      10:42
                    
                    
                      And then finally for our text area,
                      10:54
                    
                    
                      we actually put it in here,
pet.description.
                      10:57
                    
                    
                      There we go, okay.
                      11:06
                    
                    
                      Now let's hard refresh our page and
perfect, it's Dog, it's not Housetrained,
                      11:09
                    
                    
                      it is Spayed, it's Female and
there's Lucy's description, perfect.
                      11:15
                    
                    
                      Now with that setup,
                      11:21
                    
                    
                      we need to make sure we can submit
this form to the correct URL.
                      11:23
                    
                    
                      So I'm gonna scroll back
up to the top of our form.
                      11:28
                    
                    
                      And we don't want this to go to add_pet,
                      11:32
                    
                    
                      we want it to go to edit_pet instead,
let's hit save.
                      11:35
                    
                    
                      And probably remember this from last time,
we need to update our route now.
                      11:39
                    
                    
                      Take methods of GET and POST.
                      11:44
                    
                    
                      Now we can follow the same conventions
that we did earlier, so if request.form.
                      11:57
                    
                    
                      And then to set our pet values
equal to something new,
                      12:05
                    
                    
                      we need to use our pet.name is
equal = request.form with name,
                      12:10
                    
                    
                      and I'm gonna go ahead and
copy this as we go.
                      12:16
                    
                    
                      So it's gonna be a little bit of typing.
                      12:20
                    
                    
                      Let me actually check that we made
sure our alt is called alt and
                      12:44
                    
                    
                      that our Pet Type is called name of pet,
okay cool.
                      12:51
                    
                    
                      So pet, oops, that should be
                      12:56
                    
                    
                      Okay, now at the bottom we need to do
                      13:17
                    
                    
                      db.session.commit, and
                      13:22
                    
                    
                      then we went to redirect
them to the homepage.
                      13:26
                    
                    
                      url_for ('index'), awesome.
                      13:33
                    
                    
                      Let's give this a try.
                      13:40
                    
                    
                      On this let's do a hard refresh and,
oops, let's see what we got.
                      13:44
                    
                    
                      edit_pet, did we forget to specify the id,
we probably did.
                      13:50
                    
                    
                      Let's pop over into our editpet.
                      13:54
                    
                    
                      And let's pop up here into our form, and
yes, we need to throw in our pet id here.
                      14:01
                    
                    
                      id = pet.id, save, and let's try giving
                      14:07
                    
                    
                      this a refresh, there we go, okay.
                      14:12
                    
                    
                      I'm just gonna try naming
Lucy something else.
                      14:16
                    
                    
                      Let's do Luna, and then I'm just gonna
hit Submit and our name is updated.
                      14:19
                    
                    
                      And if I click into Luna,
name is still Luna, perfect.
                      14:26
                    
                    
                      Finally, let's tackle deleting an entry.
                      14:33
                    
                    
                      Back in our pet.html,
                      14:37
                    
                    
                      let's add a url for delete, url_for,
                      14:41
                    
                    
                      this is going to be delete_pet.
                      14:47
                    
                    
                      And we're going to need that id still,
pet.id.
                      14:53
                    
                    
                      And let's come over here into app.py and
                      14:59
                    
                    
                      after our edit need a app.route, and
                      15:04
                    
                    
                      it's going to be /delete/,
it's going to take an id.
                      15:09
                    
                    
                      Okay, and
then we'll need to grab our pets.
                      15:25
                    
                    
                      So pet = Pet.query.get(id).
                      15:30
                    
                    
                      And then let's check the docs
to see how delete works.
                      15:39
                    
                    
                      So I have Queries, Deleting Records,
so db.session.delete and
                      15:44
                    
                    
                      then the variable that's holding
our pet and then db.session.commit.
                      15:50
                    
                    
                      Db.session.delete(pet) and
                      16:00
                    
                    
                      db.session.commit().
                      16:07
                    
                    
                      And then let's redirect
them to the homepage,
                      16:14
                    
                    
                      (url_for( 'index' )),
                      16:21
                    
                    
                      save and let's give this a test.
                      16:26
                    
                    
                      I do a hard refresh.
                      16:34
                    
                    
                      We're already on Luna's page.
                      16:36
                    
                    
                      Now scroll to the bottom, and
if I hit Delete Luna is now gone.
                      16:38
                    
                    
                      Awesome, you've tackled reading,
updating and
                      16:46
                    
                    
                      deleting entries in your database,
and working with them using Flask.
                      16:49
                    
                    
                      Fantastic work Pythonistas.
                      16:54
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up