Join Caleb Smith for an in-depth discussion in this video Implementing views, part of Learning Python and Django.
- As we've seen, views implement the control layer…or logic of our project.…They usually make use of models…for creating the database.…And they use templates to help generate the HTML.…Let's expand on the views that…we currently have in the project.…We'll begin by opening a text editor…and opening the views.py file…inside of the inventory folder.…This file imports render already, which we'll use later,…but we need a few more imports.…First, we'll need to import http 404 from Django.http…in order to return a 404 page when appropriate.…
Also, we'll no longer need the http response object…so I'll get rid of that import.…Next, we also need to import the item model…from inventory.models so that…we can use it to query the database.…In the index view, I'd like to show the title…of all the items that are in stock.…To accomplish this, we'll use the .exclude method…we saw in the session on using the ORM.…Now, I'll type items equals Item.objects.exclude…amount equals zero.…
Now instead of using an http response object…directly as before, we'll use…
- Creating a new Django project
- Defining Django models and fields
- Querying data with the Django ORM
- Building views and templates
Skill Level Intermediate
Q: pip says it was installed correctly, and I've updated the PATH variable as shown, but the pip --version command isn't working. Why?
A: Depending on your computer's setup, you'll need to restart your terminal/command prompt before the additions you've made to the PATH variable are respected. With some setups, even that won't be enough, and you may need to restart the entire machine before you are able to use the pip command directly.