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 the App

In this lesson, you’re going to create your first Django app. The command you need to use is python manage.py startapp projects. You’re going to start by creating this app and will later flesh it out, as you saw in the first section of this course.

Head over to projects and see your page running. You’ll get a message telling you that this site can’t be reached. You stopped the development server, so it’s not currently running. You can start solving this problem by opening up an extra terminal tab and starting your development server with python manage.py runserver and just keeping it running.

When you reload the page, you still get an error message. It turns out that the URL isn’t pointing to anything. The error message points you toward portfolio.urls. Take a look at urls.py in the portfolio folder.

00:00 In this video, we’re going to create the first self-standing Django app. For this, I’ll head over to PyCharm and the command to create an app in here is going to be python manage.pyso, this is often the center where we do all our commands from—and then we’re going to say, startapp and give it a name.

00:21 So, the first app we’re going to create is going to be called projects. This is eventually going to be a list of all the projects, the web projects that you made.

00:30 We’re going to start off going through the process of building this one, and later we’re going to flesh it out in the way that we saw in Part 1.

00:41 Okay, so, it looks like nothing happened, but if we refresh this up here, similar to before, when we created our management app, when we ran this django-admin startproject, we got this management app and now with the command manage.py startapp, and also giving it a name, we get a new app in here.

01:00 And this looks different than the management app, as you can see. So now, every app that you’re going to create is going to look like this. The management app is a bit different as in that it contains the settings file, wsgi file, and the others—sorry, urls and __init__ all exist in these ones.

01:18 We already talked about the files a bit, but this is the basic structure of every Django app. So, can I now already head over to /projects, what I tried before, and see our page running? Let’s give it a try.

01:32 So I’m here, /projects. All right. So, the first thing I’m getting is This site can’t be reached. It’s not really a Django error message, it’s still an error message. What’s happening?

01:45 The thing is that I stopped the development server before, so it’s currently not running. To solve this, and then still be able to interact with our terminal, I’m just going to open up an extra terminal. Tap here, and here I’m going to start our development server: runserver.

02:03 And we’re going to just keep that one running. Okay, so now it’s live again. Move over here, reload this. All right. It’s still the same error message as before: Page not found.

02:14 So, we created this projects app, but the URL is not pointing to anything, so now let’s take another look at the tip that Django gives us here.

02:24 It’s using the URL configuration defined in portfolio.urls. Okay, so let’s take a look. I hop back over here, let me go to portfolio/ and then urls.py.

02:39 Lots of tips from Django and just so you know what’s happening here, PyCharm sometimes makes these things smaller so that you don’t see everything that’s going on. There’s still imports happening here.

02:51 So, Django defined only one URL pattern—and that happens by default—which is to the admin portal. We’re going to look at the admin portal later, so don’t worry about it as of now.

03:01 But that’s the only URL pattern that’s defined currently, so going to /projects is not going to take us anywhere, because Django

03:11 doesn’t know what to do.

03:14 So, it’s already pointing us towards where we are going to have to add something or change something in order to continue. And we’re going to do that in just a moment, in one video over this one, where we’re going to start talking about the routes. But before that, we’re going to take a little excursion and look at Django’s settings.py file.

Avatar image for reblark

reblark on Oct. 17, 2019

In this presentation and in the one from Django itself, you frequently forget to mention that you turned off the server. That is a terrible mistake for the viewer because as long as the server is running, the viewer will not get the result of localhost:8000/projects and he/she will waste of lot of time trying to figure out why they still get the other error message. You need to carefully communicate everything you do. Leaving out that little bit of information is, well…I am biting my tongue.

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 23, 2019

Before moving on to building your routes, make sure to register your app in the project’s settings.py file.

Check out the relevant section in the Supporting Materials. You’ll have to manually type the name of each app that you add to your Django project into the INSTALLED_APPS list in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'your_app_name_here',  # in this case it'll be 'projects'
]

Registering the app is necessary for your Django project to know which apps are part of the project, and for everything to interact correctly.

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 23, 2019

@reblark I mention in the video that the error message changes depending on whether the development server is running or not.

One of the most important things that I am trying to convey in this course is that while doing software development you’ll regularly run into error messages and unexpected situations. When something happens that is different than you expected, or different than documented, then it’s often a good idea to take a closer look.

Getting into the habit of handling these unexpected situations by looking for help in the feedback your program gives you and googling the parts that don’t make sense (yet) is an awesome way to deepen your understanding and train an approach to problem-solving that will come in handy over and over again.

Avatar image for reblark

reblark on Oct. 23, 2019

I appreciate this response and understand the overall suggestion. I would like to point out that many MOOCS rush to get products out, make lots of mistakes in their presentations that take hours and days to try to figure out. As I mentioned, I am running two instances of the class in order to figure things out on my own. While I buy the importance of figuring things out on your own, I believe a lot of MOOCS just avoid the responsibility of teaching. The sink or swim approach is not useful and MOOCS historically have such a high failure rate amongst their students. People simply don’t finish the course. Taking responsibility for your students is difficult and time consuming and since MOOCS are typically running on VC money, people who care about nothing but monetization, I think it is a poor business model. Your responses, and Real Python in general has been a Real Change in that and, for that, I am grateful. Thank you,

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 24, 2019

Thanks! I agree with what you’re writing. “Go fast and break things” is not my favorite motto either, but it’s often applied in the tech world. ¯\_(ツ)_/¯

But our courses here are definitely not meant to be sink or swim. However, there’s always something that we might miss or not explain in the best way when creating a course. So it’s helpful when someone points out parts that ended up confusing because it makes it easier for future students and also gives us a chance to add an explanatory note.

Thanks for your work, and keep it up!

Avatar image for reblark

reblark on Oct. 29, 2019

Thanks for this response.

Avatar image for jeffwashcloth

jeffwashcloth on June 4, 2020

What directory are you typing in “python manage.py startapp portfolio?”

Avatar image for jeffwashcloth

jeffwashcloth on June 4, 2020

Sorry, above question should read: What directory are you typing in “python manage.py startapp projects?”

Avatar image for Martin Breuss

Martin Breuss RP Team on June 5, 2020

Hi @jeffwashcloth, you need to type this command inside of the folder where your manage.py file is located, since you are telling python to execute that file.

Avatar image for bgersten

bgersten on Nov. 25, 2023

Hi Martin, When examining the contents of urls.py in Pycharm at time 2:55 of the video, the two lines of imports never appear, lookimg like:

import…

urlpatterns = [ path(‘admin/’, admin.site.urls), ]

but a cut and paste of the same area of Pycharm looks like:

from django.contrib import admin

from django.urls import path

urlpatterns = [ path(‘admin/’, admin.site.urls), ]

Is this just a quirk of Pycharm or does my setup need adjusting?

Avatar image for bgersten

bgersten on Nov. 26, 2023

Problem solved: there was a faint expansion symbol ‘>’ in the margin next to the ‘import’ line. On clicking the ‘>’, the two import lines appeared. Simple!

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 6, 2023

@bgersten such stuff is sometimes the hardest to spot! 🔍 Nice sleuthing!

Become a Member to join the conversation.