Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Debugging: Part 2

In the previous lesson, you saw that FilePathField() might not be the best choice for your model, so you’re going to change it:

Language: Python
image = models.CharField(max_length=100)

When you check your page again, you’ll see that nothing changed. This is because you just made some changes to models.py but didn’t modify the database. In order for the changes to be made in the database, you need to makemigrations and migrate.

00:00 In the previous video, we ran into the problem that we realized FilePathField might not be the best choice for our model here. We don’t actually need it.

00:08 So we want to go ahead and change this. However, there’s a couple of things that we have to consider when doing this, so let’s go over it step by step. In our case, we’re really just saving a couple of characters: /projects/img and then the filename. So I will change this to be a CharField (character field), instead.

00:29 We can get rid of all of this and just give it a max_length argument. Our path shouldn’t be—they are always going to be projects/img/ and then the name of your project, so they should never be longer than 100 characters. Okay.

00:43 That’s a simple change. Now, what does it mean for us? I’m going to exit the Django shell. Did this already do our changes? Let’s go ahead and check. Reloading this, nothing changed.

00:57 And you might wonder: why is that the case? I mean, in this case, we actually didn’t change anything, we just changed the data type, right? But also, we did not do any changes to the database at all.

01:10 We just did some changes to the models file, but that doesn’t mean that the changes are propagated to the database. So, in order to make these changes to the database, remember, we have to do two things. First, we have to makemigrations to create the migrations file.

01:25 That’s going to then translate into SQL and make the changes to the database. And two, run the migrate command to then actually apply the changes to the database.

01:36 Let’s go ahead and try that. I did my changes in here to the models file. Next, I’m going to go and say python manage.py makemigrations.

01:49 Here, we can double-check. Okay, migrations for the projects app. What happened is we altered the field image on project. Yes, correct. That’s what we did.

02:00 So, let’s apply those migrations, and let’s see what happens now.

02:10 Cool, so it applied the migrations for 0003everything is fine. Let’s take a look at our database right now. Great. We applied the changes. So, does that mean our project works?

02:28 Of course not, because we’re still just pointing to testproject.png without giving the full path. Okay, so there’s some more debugging that we need to do.

02:39 What we need to do is we need to change the paths for those entries that we already have in the database, so what we’ll do is we will go back into the Django shell.

02:53 We’re going to pull, again, all our projects and we’re going to simply change what’s saved in the database.

03:13 Had an encounter with a little friend here. ps from the project 0let’s save it to its own variable p1 (project 1), the first one, and p2 (project 2), the second one.

03:30 Okay. So p1.title is 'test project', and p1.image—the path is just 'testproject.png'. Cool. Now I’m going to go ahead and change that.

03:42 I’m going to say p1.image is instead going to be 'projects/img/testproject.png'.

03:58 Save that to the database.

04:03 And let’s take a look whether that changed something here. Here it is! Now we’re displaying the right path: projects/img/testproject.png. Django can find the static file and finally can also display it.

04:16 Our second one still does not show up, and the reason is because we haven’t changed the database entry yet. I’m going to go ahead and do that for the number 2, also. Let’s choose a different one—maybe that todo.png image. projects/

04:37 Make sure that you save it. If I do not save it and go back over here, we did not yet apply the change to the database, so nothing changes here. However, once I go and say .save(), my changes that I did here in the console are written back to the database and now we have a different path in here and finally, we’re able to display that image. Great!

05:00 Now it starts to look a little bit like our final version. We went ahead and did an intervention inside of our models. Like, we went back into the models and changed something, and we went to directly interact with our database using the Django shell and changed entries in there.

05:21 At this point, just a quick word of warning: changing column data types, as we did just before in the models, does not always go as easy as it did for us.

05:30 We simply went and changed this FilePathField to a CharField, made the migrations, and applied the migrations to the database and everything was fine. However, in some cases, this is not going to work as easily, and that’s when you’re actually changing the data type.

05:46 So, the FilePathField is also just a character field, so there were no big changes that we had to do, but if you already have something sitting inside of your database and you apply some changes like this, you might run into some errors from Django. What you can always do—especially when you’re working on a small project and if you’re still in development—you can always go ahead and simply delete your database.

06:10 That’s often the easiest way: just right-click, delete it, get rid of it, and then make your migrations again. Again, makemigrations and run them, and Django is simply going to create a new database file for you.

06:24 Of course, it means that you’re going to lose what’s written in there so far, but that’s why it’s important to test often and test early to make sure that the stuff works in a way that it does, and so that you don’t yet have your meticulously-crafted projects in there.

06:38 But we’re currently just working with some test data, and then it’s no problem to start over again. There’s other ways of solving a problem like that but I’m just giving you this tip that often, especially for simple projects in development, it’s easiest to just delete and start over. Okay.

06:55 That’s it for our debugging excursion. We did some cool changes. We made it work. We can finally see our images and we’re ready to move on to work a little bit more on our templates. See you in the next video.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on June 5, 2023

@simma99 Is it possible that you already made migrations but haven’t applied them yet? There could also be a problem with your models, but it’s just guessing on my part.

Avatar image for sammyutere

sammyutere on Aug. 26, 2023

Hi, Just letting you know following your instructions step by step, I did not get my displayed as you got. So I think there is something definitely missing somewhere, that you have not covered.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 28, 2023

@sammyutere Thanks for letting us know. Could you please share a little bit more details about what’s not working and what specific steps you’ve taken following the instructions? What is the discrepancy between the expected and actual results? This way, we’ll be able to better figure out what the issue may be.

Avatar image for sammyutere

sammyutere on Aug. 31, 2023

Hi, In the section Debugging part 2 from the 2 min mark, when I followed the instructions as you did to display the project image. It did not work for me.

Avatar image for deebrown2011

deebrown2011 on Jan. 22, 2024

Hi, i’m wondering if somebody can help? i was having some issues with information being correctly displayed in my server after making changes made following the above video. anyway, long story short. i deleted the db.sqlite3 and re-run the migrations etc and now i have a new table but my models.py info is not showing in new sqlite3 database eg, title, description…etc. also my webpage is now blank when refresh it, so it looks like me app is not being recognised. any ideas on how to reinstate the project?

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 22, 2024

@deebrown2011 if you deleted your db.sqlite3 file, then you also deleted all the data in it. That means that your new database will be empty until you add new data to it, for example using the Django Shell like I did in this course.

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 22, 2024

@sammyutere can you give us more information on what exactly you did and what happened instead of showing the image? That’ll make it easier to figure out what might have happened.

Avatar image for deebrown2011

deebrown2011 on Jan. 23, 2024

Hi Martin, thank you for your response. much appreciated.

your response was exactly the solution i was looking for! i was searching for while and the answer was simple.

really appreciate the help.

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 23, 2024

Glad it worked @deebrown2011 :D

Avatar image for Fahim Mahmud

Fahim Mahmud on May 21, 2025

So, using FilePathField(path = ‘/projects/img’) to add image file to database by simply putting the image file name (testproject.png) is not possible? we have to use CharField(max_length = 100) and use the full path? Is there any other way?

Become a Member to join the conversation.