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

Django Shell

Your next step is to put data in your database. For this, you’re going to use the Django shell to put one project in your database. Head over to your terminal and start the Django shell by typing the following:

Language: Shell
$ python manage.py shell

This opens up a terminal, but it has the Django settings already imported, so it allows you to work directly from the root folder of a Django project.

00:00 Next, we want to put some data into our database. For this, we’re going to use the Django shell. There’s some different ways of doing that, but I want to show you this very handy tool. We’re going to get started and put one Project into our database.

00:15 For this, I head over to my terminal, in here. I’m going to start the Django shell by typing python manage.pyas so often—and then shell. This opens up a terminal.

00:30 However, it has the Django settings already imported, so it allows you to work directly from the root folder of a Django project. So, if you want to do something within Django or with the database, then start your shell like this. Now, what I want to do is I want to create a Project object and put this one into the database. For this, I’m going to use Django’s ORM. So, here is our cheatsheet.

00:57 That’s what a Project instance is going to look like. It’s going to have a title, description, technology, and image—an image path.

01:05 We put the images already in here. So, let’s go ahead and just create one. What we need to be able to use, then, is we have to import this Project model.

01:16 We can find this inside of—let me close this for a moment—projects/ and then models.py. Okay, so this is where it sits. I’m going to say from projects.models import Project, and now I can create a Project.

01:37 So, my first project is going to be a Project instance that has a title.

01:45 It’s going to be "test project".

01:50 It’s going to have a description

02:03 Yada, yada, I’m just going to fill this out. Make sure that you put in the right data types. Like, it’s clear that we’re going to use text for a CharField (character field), TextField, and you can’t go over the max_length or it’s going to complain, et cetera.

02:28 And for image, I’m just going to put in

02:32 the name of the image file as the rest of the path—you remember Django knows about static/ folders in all apps, and we told it about projects/ and img/, so we directed it from here to img/.

02:45 All I need to put in here is, really, the name of the image file. So, I’m going to say "testproject.png". Okay. This is my first Project instance. Let’s create it.

03:02 Cool. So, all done? Not quite. What we need to do first—similar to the migrations—there’s always a step in between before you actually commit something to the database. So what I need to do to put it into the database is I have to say p1.save().

03:22 And now, I have this one instance as a row sitting inside of my database. We can inspect this with this visual tool that I showed you before, go ahead and do that. I’m going to show you a different way of doing it, which is I can query here. So, I can say results = Project.objects.all().

03:43 With this, I’m retrieving all the Project objects from the database, and now I can look at this. It’s telling us it’s a QuerySet instance, as a result, and it contains one Project object.

03:58 Awesome! I can say, “Give me that first one.” I can say results[0] is going to be my p1let’s call it different, just to—p, for this one.

04:13 I can say p.title, for example, and this is 'test project'. It’s what I just put in there—so, what I did. And that’s where we can see the power of using an object-relational mapper. Using Python, I created an object, then I simply said .save() to put it into my relational database, and here I’m—again—using the ORM to query the database, get some results back, and then you can see, I can access them again very similar—or, in the same way that I would access a Python object.

04:47 So that’s the ORM at work, and yeah, we put a Project inside of our database. So, let’s make sure to set up the rest that we need. We still need routes, we need views, and we need templates. Let’s make sure to set those up so that we can actually see this Project, also on the front end.

Avatar image for James

James on Jan. 14, 2021

@Bartek - very cool, thanks! I appreciate the example and links.

Avatar image for gburbano92

gburbano92 on Jan. 26, 2021

Hi, I’m facing some difficulties having the “daily”, “test project”, “todo” images appear in the img file. I’ve completed as instructed the double file projects>img but no images, through my desktop I can see the files are present in my django portfolio>projects>static>projects>img file so not sure what’s causing the no show in my virtual environment.

Avatar image for crispineda34

crispineda34 on March 23, 2021

For this command I keep getting this error. I’m not exactly sure on how to proceed. “Model class projects.models.Project doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.”

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on March 23, 2021

@crispineda34 If you google that error message and follow the first search result, you’ll land on a StackOverflow question, which seems to have the right answer for you:

Are you missing putting in your application name into the settings file? (…)

Avatar image for applejfl

applejfl on Nov. 28, 2021

I can’t get pass this:

from projects.models import Project
>>> p1 = Project(title='test project', description='this is a test, 1234!', technology='Django', image='testproject.png')
>>> p1.save()
Traceback (most recent call last):
  File "C:\users\apple\documents\django-portfolio\.env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\users\apple\documents\django-portfolio\.env\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in e
xecute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: projects_project
# models.py

class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.FilePathField(path='/projects/img')

Thanks!

Avatar image for applejfl

applejfl on Nov. 28, 2021

Nevermind, I found the problem

Avatar image for mochibeepboop

mochibeepboop on Sept. 9, 2023

Wonderful course :) I have a question: Why is the path to the image in models.py not projects/static/projects/img And instead is /projects/img

Here is the entire code in projects/models.py, I am referring to the last line:

from django.db import models

class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.FilePathField(path="/projects/img")

Thank you!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Sept. 11, 2023

@mochibeepboop Great question! It’s because Django automatically looks for static files in each application’s static/ folder, so you only need to provide a path relative to it.

Avatar image for mochibeepboop

mochibeepboop on Sept. 11, 2023

Thank you for your response @Bartosz Zaczyński! So do we essentially get ‘projects/static/’ for ‘free’? Meaning Django prepends this part of the path in the background?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Sept. 11, 2023

@mochibeepboop Yes, that’s exactly right.

Become a Member to join the conversation.