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

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:

Language: Shell
$ python manage.py shell

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

Language: Python
>>> from projects.models import Project

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

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

00:00 Let’s go ahead and look at all these things displayed now. We’re here and this is how it looks like: we have our title, our description, and the technology—everything displayed in here. Now, how would it be if we have some more?

00:16 We don’t only have one project in there, but we want to see whether our for loop works and displays all of the projects that are in there, so let’s go ahead and add some more data, again using the Django shell. To start a Django shell, if you remember, we go and type python manage.py shell,

00:40 and this opens up the Django console for us. So now I’m able to interact using Django’s default settings already opened up. I want to be able to add something to our projects model.

00:55 I’ll open this up so we have a reference. I’m going to have to import it first, so I go to projects.models, remember? from projects

01:09 and now I’m able to create another Project instance that has a title,

01:18 description,

01:25 technology—still "Django"and an image.

01:33 I’m just going to give it the same image for now. We’ll talk about these images in a bit. Okay. I created this Project. We can take a look at it. Yes, the instance exists.

01:46 We can access the .description or any other attributes that we have on it. So now, what we need to do next is we need to save it, so I call the .save() method on it and now we should have it sitting inside of our database already.

02:03 So, without needing to exit any of this, I could just head back over and reload this. And there it is! You see? We have now two objects in our database. The QuerySet returns both of them. We’re passing them forward to the template and our for loop inside of the Django template renders the first one, and then the second one, picking out the different attributes of the object—there’s .title and .description, .technology—and putting them into whatever HTML constraints we created for them. So, this one is inside of an <h1> HTML tag, and this is a <p> element in here, et cetera, et cetera.

02:42 Cool! So, that’s how we build templates in Django with the Django templating language. We’ve used template variables in here, and we’ve used template tags for code logic that look like this simple for loop, in this case. Sweet! And that worked great.

02:59 In the next video, we’re going to get to know some more developing with error messages. We’re going to take a look at what we did with the FilePathField, and then fix something that has come up here during development.

03:15 Remember, that’s totally normal and fine, and we’re going to learn how to deal with such situations. See you in the next video, and get ready for debugging!

Avatar image for Martin Breuss

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'
Avatar image for Lokman

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!

Avatar image for kumimochi

kumimochi on July 29, 2020

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

Avatar image for Martin Breuss

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 :)

Avatar image for alcuinog

alcuinog on Nov. 26, 2020

Could you please provide me with a link to the list of Django shell commands and list of Django template language ?

Thanks. Alcuino

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Nov. 27, 2020

@alcuinog If you type one of Django’s built-in commands, you’ll get the list of available subcommands:

$ django-admin

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    (...)

To get more details, you can try the help command:

$ django-admin help shell

Alternatively, use the --help flag:

$ django-admin shell --help

The official Django documentation is pretty decent. You’ll find everything you need there, including help on the Django template language:

docs.djangoproject.com/en/3.1/ref/templates/language/

Avatar image for thomasseidling

thomasseidling on Dec. 28, 2020

Hi Martin, . Great content. Going through the course I was wondering whether you could provide a working Portfolio project as a sample. The once that’s in the course materials section is only partially written.

The reason for asking is that I am visually impaired and therefore can only follow the audio but not the video. Piecing together certain things from the audio only is quite challenging and time consuming, e.g. ‘now we change this down here …’ only makes sense together with visual information. If I could use a finished project as reference point that would be very helpful.

thanks in advance

Avatar image for Ricky White

Ricky White RP Team on Dec. 28, 2020

Hi @thomasseidling,

Thanks for your feedback. I’ll make sure Martin gets it. We have a written version of this course which might work better for you, in this instance, if you use a screen reader? This is the link to the article: realpython.com/get-started-with-django-1/

There is also downloadable source code linked in the article. I hope that helps.

Ricky

Avatar image for thomasseidling

thomasseidling on Dec. 29, 2020

@Ricky, thanks, very useful!

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 29, 2020

Hi @thomasseidling, thanks for your feedback and thanks @Ricky for posting the article link.

I’ll definitely try to be spell out more descriptively what I’m doing on screen for my next videos. 👍

Become a Member to join the conversation.