When you created
models.py, you said that there would be images associated with each project and you put them in the database with the filepath
# Create your models here. class Project(models.Model): title = models.CharField(max_length=100) description = models.TextField() technology = models.CharField(max_length=20) image = models.FilePathField(path='/img/')
Just like you saw with the
templates folder, Django is aware of all the
static folders inside each project and collects all of them and puts them all in one folder, so don’t forget to use double folder structure.
Django’s going to know about that this exists, and it’s going to look for images, for example, inside of there. So, what we’re telling it here is we’re saying inside of static, there’s going to be a folder called
img. Let’s create that one as well.
So, what we could do next is just copy-paste those images right in here, and then start hooking it up. However, what I mentioned for a moment was these
static/ folders are similar to
templates/ in that Django is aware of them inside of each app, and they are similar in another way, which is that in production, Django collects all the
static/ folders from all of the apps that you have in your project together, and puts them all into one folder.
So remember, we looked at Django’s double folder structure when we looked at template folders. So we said, we want to put always a subfolder that has the name of the project inside of the
templates/ folder, so that then, when they finally all get collected together in the big
templates/ folder, we’re not going to run into any problems.
projects/, we’re going to have a
static/ folder, and inside of there, we’re going to have another
projects/ folder, named after the app that you’re working with. The reason the exact same as with
templates/: if it gets collected together, we want to avoid ambiguity.
So, eventually, this is what we would want to end up in production: one big
static/ folder, a subfolder named after the app, and then inside, whatever we have, and then we can repeat ourselves and it’s not going to make any problems. Okay, so, this is not how we currently have it set up over here, all right?
that’s how we want it to look like:
'/projects/img'. And then, inside of here, we’re going to recreate this same structure that we have with
templates/, so I’m going to make a new folder in here, call it
projects, the name of our app, and then I’m going to move this folder in there.
03:28 So now, perfect, right? Now we have the double folder structure, everything is fine. Inside of here, we’re going to have our images. However—and that’s an important thing to be aware of—we made a change to our model that is going to tell the database how to set it up, but we have not applied this change to the database.
So what we need to do to make these changes also be reflected inside of the database—so that every data item, every
image file path that we’re going to add, is actually going to have this extra
projects/ folder prepended to it—is we going to have to do two things. First, we’re going to have to say
python manage.py makemigrations.
04:33 A way that you can see here, also, is a second migrations file popped up that has encoded in Python to translate into SQL, and then finally apply those changes to the database. Again, they’re not applied yet.
by running the command
migrate. So, once I run this, we see a much shorter output than last time, but we get another
OK, so that means that now our database, every time we’re going to add something to it, it’s going to prepend
'/projects/img', which is perfectly correct for how we want our double folder structure to be set up. All right. So, let’s close all of this.
We took a little excursion in here. What we want to do now, is to also add image files in here. Because all that we’re storing in our
FilePathField, is just the path to the files, not the files in themselves.
05:38 We’re going to have to add the files to this folder. And for that, I’m going to head to my desktop, where I have these three files randomly distributed around here, and I’m going to add those to my project.
06:24 So, after moving them over, now I have them sitting in here, just inside of the folder structure. We know that our path that we’re saving into the database is going to correctly bring us all the way to here, so that what we’re going to have to add is only the filename—depending on what entry we want to choose, the correct filename for that.
Become a Member to join the conversation.