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 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.

Go to views.py, where you already have some imports as well as a helpful message to create your views here. Create your new function, project_list():

# 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!

Comments & Discussion

Paul on Oct. 28, 2019

Something to note here: When I was building out this new view, I wasn’t getting the same error messages in my browser, I was seeing a PageNotFound error instead.

After getting to know this “friend” a little better I realized that I had the wrong url in my address bar. I was still at the home page of http://127.0.0.1:8000/instead of where Martin was actually at, which was http://127.0.0.1:8000/projects/

Martin Breuss RP Team on Oct. 29, 2019

Awesome, great job figuring this out @Paul and thanks for posting here. There’s always something to learn when you get to know your friends better ;)

Kevin Lao on Nov. 19, 2019

Hello Martin,

On that note, is there a way for us to resolve the ‘Page not found’ friend when we just go to 127.0.0.1:8000/?

Martin Breuss RP Team on Nov. 23, 2019

Yes, there’s a way to do that–you’ll have to create a urlpattern for the empty path / (without anything after) and write a view that handles that path and returns something.

As you don’t have anything defined for the empty path, it can’t find the page and tells you, quite literally: Page not found.

Tumise on Dec. 13, 2019

hello martin would it be alright to create all both my apps and projects in my virtual enviroment folder?? i seem to have created it there and everything works fine for now

Martin Breuss RP Team on Dec. 14, 2019

@oladelealade I suggest to keep (or move) your apps and projects folders outside of the venv folder. One reason is that you might want to .gitignore your virtual environment folder before committing to version control, and then you couldn’t add any project code.

I suggest to aim for the following structure:

rp-portfolio
├── .env/
├── manage.py
├── portfolio/
└── projects/

Ekundayo Blessing on Jan. 5, 2020

I got the two urls.py files mixed up. Fixed now 😀

Martin Breuss RP Team on Jan. 7, 2020

Well done @Ekundayo Blessing! :)

Brandy Wright on March 3, 2020

For whatever reason, my “projects” app already had a views.py so I didnt get the same errors. Here is what it contains currently:

from django.shortcuts import render
from projects.models import Project


# Create your views here.
def all_projects(request):
    # query the db to return all project objects
    projects = Project.objects.all()
    return render(request, 'projects/all_projects.html',
                  {'projects': projects})


def project_detail(request, pk):
    project = Project.objects.get(pk=pk)
    return render(request, 'projects/detail.html',
                  {'project': project})

I’m going to omit the steps and make no changes yet.

Martin Breuss RP Team on March 5, 2020

Hi @Brandy Wright. If you downloaded the .zip file you get the complete code of the finished project. If you want to build it alongside the course, which is what I suggest in order to get the maximum learning benefit, then you should build the project from scratch. Think of the .zip file just as a reference project you can look up stuff if you get stuck.

emalfiza on March 18, 2020

Absolutely loving your clear and well explanation of django URLs…anywhere else I was not sure of the logic, I am so grateful of you @Martin Breuss.

Regarding the UnresolvedReference PyCharm to give some hints, but is there any other way to find out more about!

npujari on May 8, 2020

Hi Martin, I am following along your steps. I am getting this error message.

django.core.exceptions.ImproperlyConfigured: WSGI application ‘portfolio.wsgi.application’ could not be loaded; Error importing module.

npujari on May 8, 2020

Disregard my previous question. I was able to fix it. Thanks.

Andrew on May 26, 2020

Oaky, I did not see “Hello, everyone!” on your django.

Martin Breuss RP Team on May 28, 2020

Hi @Andrew. Check out the video around 5:30, which is where it shows how “Hello everyone!” gets displayed after passing the text to the HttpResponse() object.

Become a Member to join the conversation.