Create a View
In the previous lesson, you set up the routes that directed your request all the way to your
views.py file. You got an error telling you that this views doesn’t actually exist. Now, you need to create a view.
views.py, where you already have some imports as well as a helpful message to create your views here. Create your new function,
# Create your views here. def project_list(request): pass
Now you’ve got a new error message that you can use to fix your function!
In the previous video, we set up the routes that directed our request all the way to our
views file, but then we ended up getting an error that told us that “This view doesn’t actually exist.” So, what we’re going to need to do next, is we need to create a view.
This is where I directed it to. However, inside of
views, there’s no such thing. So, I want to take this as a moment to just take a look over to the browser and see, is Django maybe giving us an error message there?
They’re either presented in here, in the browser, with this nice yellow bar up top that we saw before, or—if you’re just getting a browser error here—you head over to the terminal tab where you’re running your development server, and then you’re going to have an error message sitting in here. In this case, that’s what we’re having. And as I said before, it’s telling us, “Okay. Inside of the
views file, we’re directing to a function that does not exist.” So, let’s change that!
I’m heading over to
views. Again, you see there’s already some imports done by Django, and it’s telling us with this helpful message here,
Create your views here. Let’s go ahead and do that. We’re looking for a view called
project_list, I’m going to say
02:00 Okay. So, I created this function here now. It currently doesn’t do anything, just passing in here, but it exists. So, let’s see what our server does. It automatically reloads, and it seems to be fine!
And there it is! We’ve got a new error message! This one is called
ValueError at /projects/. Let’s read what Django is telling us:
The view projects.views.project_list
didn't return an HttpResponse object. It returned None instead. So, good things about this: the first thing, it’s pointing to the right thing, like we know we’re going into
projects.views, it’s accessing the function that we just created.
If I head back over to my text editor, this is what’s happening. It’s going all the way to this
project_list(), it’s finding this. However, the problem was—here we have it again—the view did not return an
HttpResponse object, but
Okay, so, “What is this
HttpResponse object?” is what PyCharm is wondering. The problem is that we have not imported it yet. So I go ahead and—you can always look this up if you don’t know where to find it, but I’m going to tell you now it sits in
04:26 and now I’m returning an empty one. Let’s see. The error seems to be gone. Our server is telling us, “Okay, it’s fine.” There’s no error sitting here, which always gives us a chance to look back
into our browser and do a reload. Okay, we’re not getting an error. We’re also not getting anything else. We’re just getting an empty page. So in this case, this means that everything’s going fine and we’re returning an
HttpResponse object. That’s what we were asked to do.
05:48 So, that’s pretty cool, right? And just to show you that I can pass any sort of HTML in here, I’m going to sneak off to the internet for a moment and paste in something here for you to see. See you in a moment.
I need to scroll for a while to get back up on top. There we are. All I’m doing is I’m returning still this one
HttpResponse object, only now I’ve pasted a whole bunch of HTML in here. And that’s fine.
07:15 There’s no error, and we’re actually getting images displayed and text displayed. Everything’s here, just the styling is a bit off because we’re not linking to files that the pages are usually linking to.
07:27 So, you can see that this is a way that you could actually build a simple HTML page. However, it’s really not very sustainable, and pretty messy to have all this HTML in here. We want to have a separation of concerns.
07:42 We want to keep the HTML somewhere else than the Python code that we’re writing, so what we’re going to do in the next video is we’re gonna create—instead of pasting the HTML directly in here, we’re going to separate this functionality off into templates and create an HTML template for our HTML. See you in the next video!
Become a Member to join the conversation.