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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Create Your Templates: Part 2

Take a look at how your template is displaying. Your project page is showing the title, description, and technology! Now let’s use the for loop to display all of the projects. You’re going to add data through the Django shell. To start the Django shell, type the following:

$ python manage.py shell

You need to modify models.py. You’re going to have to import:

>>>
>>> from projects.models import Project

Now, you’re able to create another instance of Project:

>>>
>>> p = Project(title="test number 2", description="this is about more testing yes yes!", technology="Django", image="testproject.png")

Comments & Discussion

Gascowin on Oct. 20, 2019

Very well taught material good sir! Thanks for showcasing how data can be added to the database from the Django terminal. I imagine there should be a way to add these more efficiently?

Gascowin on Oct. 20, 2019

…or better still, give a user the chance to add this info from the web interface.

Martin Breuss RP Team on Oct. 21, 2019

Hei @Gascowin! Yes, there are ways to do both of these things. However, they both open up yet another chapter that can be a great next step in your web dev journey :)

Adding Data With a Script

All the code in Django is just Python, and Django Models, which provide the interface through Django’s ORM, are just classes. That means that you can interact with your Database directly by writing Python code.

Depending on where you have your data stored, you could write a script that takes e.g. CSV or JSON as an input file, fits the data into the Django Models by creating class instances (just as you did in the Django shell in this part of the course), and then uploads it to the database using the .save() method on the instance.

That said, there are also tons of Django plugins that implement such functionality, so if you have a standard task to achieve, there’s probably a solution out there. E.g.:

Allowing Users to Add Data

This one can be a little tricky, but it’s a great next step! You’ll have to work with HTML Forms and Django Forms. These are your link for allowing user input from the front-end of your page.

It’s a common functionality for many websites, so for many web dev projects you’ll eventually need to learn how to do this. However, before you go down that road, here’s one more option to consider:

Using the Django Admin for Data Input

Check out the video on the Django Admin Interface later in this course. Django comes pre-packed with this handy admin interface that allows admins to easily input data through the front-end.

You can customize it, have different permissions for different groups etc. If you want, you can read more about all the possibilities it has in the official docs.

reblark on Nov. 2, 2019

Hmm. Your html file, all_projects did not include images, yet in the shell, you entered p.image. There was no attirbute “image” and nothing showed up on the page. In models.py you do have an “image.” Was the shell line supposed to create a line in the html file and, if so, why didn’t it?

Martin Breuss RP Team on Nov. 3, 2019

Hei @reblark. Adding an entry to your database using the Django shell should never make any changes to the HTML file. Imagine the HTML file as the filter-structure that defines which pieces of information from the database you’ll actually want to display on the page. There can be more in there, but in the template you choose what to show.

You’ve added an .image attribute to your Project object because that’s a required attribute for any instance of that type of object. It is required because you defined it to be when writing the Project model in models.py. So that’s why you need to enter it in the Django shell.

Our filter (=the HTML and Django templating language code in all_projects.html), however, isn’t (yet) written in a way to do anything with the .image attribute of a project.

Watch the next video to see how to add the image by changing the code in all_projects.html.

Lokman on Jan. 7, 2020

Hi @Martin ! previous video tutorial we insert the data information to database. Then if I want do amend, how to access back to do the amend? Since we create P1 as object with shell django or I just override that object(p1)?

Martin Breuss RP Team on Jan. 7, 2020

@VeLoct84 Yes, you can amend the information of your database entry also through the Django shell, similar to how you created it. However, you will first need to fetch it from the database to use it as a Python object with Django’s ORM, then edit it as needed, and finally save it back to the DB.

Let’s take a look at how to do that:

1) Fetch The Data

As you can see in the Video, each project gets an auto-incrementing ID assigned automatically upon creation. Django does that for you, and you can see the ID the project object has in the database e.g. through the default output you can also see on the screen in the video:

<QuerySet [<Project: Project object (1)>, <Project: Project object (2)>]>

This output tells you that you have 2 rows in the database, one with the ID of 1 and the other one with the ID of 2.

Now you want to change the first row, so you’ll need to gain access to the data stored there through the ORM. The code for doing that with Django’s ORM is as follows:

p1 = Project.objects.get(pk=1)

You’re passing the primary key, which in this case is the ID of the object, as an argument to the get() method.

Now you have access to the stored data in a Python object, e.g.:

>>> p1.technology
'Django'

2) Edit The Data

Having access to the data in a Python object allows you to edit it in the familiar way:

p1.technology = "Python"

By using the code above, you can double-check that the data has changed.

3) Save The Data

Finally you’ll have to save the changed object data back to your DB. You do that with the same command that you use for saving a new object:

p1.save()

The Django ORM object remembers the unique identifier and therefore updates the existing row in your DB, instead of creating a new object.

Hope this helps and clarifies how to edit existing DB entries via the Django shell.

Lokman on Jan. 8, 2020

Base on tutorial course:

>>> db = Project.objects.all()
>>> p1 = db[0] # indexing list
>>> p1.technology
>>> 'django'
>>> p1.technology = 'python'
>>> p1.save()
>>> p1.technology
>>> 'python'

I think primary key method can simplified the code

>>> p1 = Project.objects.get(pk1)
>>> p1.technology
>>> 'django'
>>> p1.technology = 'python'
>>> p1.save()
>>> p1.technology
>>> 'python'

Martin Breuss RP Team on Jan. 8, 2020

Yes, these are two different ways of doing the same thing, and the second one can be cleaner and more intuitive if you know the primary key of the object you want to access. Thanks for summing it up :)

Two Notes

Typo In Argument Passing: The first line of the second code block needs to read:

p1 = Project.objects.get(pk=1)

You’re passing the number 1 as a keyword argument to the get() method. That’s most likely just a typo, but I wanted to make sure no one gets confused when trying to use your code :)

Verifying Changes: Accessing the object (p1) again after calling p1.save() doesn’t tell you anything about changes to the content of your database. You are again just checking up on the p1 object you created and edited before.

If you want to verify that the data changed in the database, you’ll need to make another call to the database before, like so:

>>> p1 = Project.objects.get(pk=1)
>>> p1.technology
'django'
>>> p1.technology = 'python'
>>> p1.save()
>>> # now we fetch the database row again to see whether it changed
>>> p1_check = Project.objects.get(pk=1)
>>> p1_check.technology
'python'

Lokman on Jan. 9, 2020

>>> # now we fetch the database row again to see whether it changed
>>> p1_check = Project.objects.get(pk=1)
>>> p1_check.technology
'python'

Ok, that’s awesome to check the latest update in our database. Thanks!

kumimochi on July 29, 2020

after I added the second ‘project’ on the shell, I can’t connect anymore.

Martin Breuss RP Team on July 30, 2020

HI @kumimochi what is the error message that you see? Try to exit the shell and go back in again, it might solve the problem. Otherwise please give some more info on what is happening and what feedback Django is giving to you :)

Become a Member to join the conversation.