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

Build Your Routes

In this lesson, you’re going to look at how to build routes. In the previous lesson, you tried a URL that doesn’t exist and got an error message that told you to check portfolio.url.

When you check that file, you can see all of the URL patterns that exist there. If you try to go to /projects, then you’ll see that it isn’t registered anywhere, so Django doesn’t know what to do with it. Fortunately, Django provides a lot of useful information in the comment at the top of that file.

00:00 Let’s get ready building our routes. So remember, we tried this /projects/ up here, we tried a URL that doesn’t yet exist, and we got this error message telling us, “Okay, okay. This is not working. Can’t find anything here, but why don’t you go ahead and check in portfolio.urls?

00:19 Maybe you should do something there.” So, over here in portfolio.urls, I’m taking a look and it tells me all the urlpatterns that currently exist is one path to /admin, and that directs forward to somewhere else.

00:36 But what I tried here was to go to /projects, and /projects isn’t registered anywhere here, so Django doesn’t know what to do with it.

00:46 But we can already see that it’s probably going to work in a similar way to how this 'admin/' works up there.

00:53 One thing that Django’s great at is—not only the error messages—but also just information that’s included when you start a new app or a new project. Really, all you need to know is written up here in this multiline comment.

01:08 We’re going to head straight to the last one that says Including another URLconf (URL configuration). And literally what I’m going to do here is just to read the instructions.

01:16 Django is telling me Import the include() function: from django.urls import include, path. We have from django.urls import path, so I’m just going to add this import include, path.

01:32 All right? PyCharm’s telling me I’m not using this yet, but we’ll get there, just give me a moment. Second, Add a URL to urlpatterns. So here, I’m going to make a new path().

01:45 You see, the structure is very similar to the one that we already have. We can even go ahead and just copy this. I’m going to take this, that’s what Django suggests me to do, is make a new one: path(), include().

02:02 Okay. But our app name is not blog. That’s just an example up here, so we’ll have to replace this here with our actual app name. I’m going to say 'projects/',

02:17 and that’s it. Now, include() is used, and we replicated exactly the suggestion that Django gave us up here. We made a new path() and it’s pointing to the URL projects/, and what we’re going to do next is we’re going to include projects.urls.

02:34 Okay, is that it? Let’s take a look! So, I’m over here again,

02:42 and I’m trying to call to this /projects/ URL, and I’m getting a new thing. So it’s telling us, refused to connect. Think of this as a new friend, right?

02:50 We’ve got a different error message. Again, this is a browser error message, not really a Django one, but same story all the way.

02:58 Heading back over here and finding out why is that happening, here it tells me ModuleNotFoundError: No module named 'projects.urls' exists. Remember?

03:11 This is one of our new friends. ModuleNotFoundError: No module named 'projects.urls'. It’s kind of self-explanatory, but it’s still a bit scary to see it sitting around like that, so let’s try to pick it apart.

03:27 Django seems to be looking for a module named projects.urls. That comes clearly from what we said here. We told it to include something that’s sitting inside of the projects app, and then it’s called .urls. Remember seeing that before, when it told us to look into portfolio.urls in the error message?

03:48 So, it seems that it was finding it there. So what we want to do is essentially just replicate this. We don’t currently have a urls file sitting in here.

03:58 That’s the module that it’s looking for. Okay. So what I’m going to do is inside of the projects app, I’m just going to create a module, a Python file, that I call urls.

04:13 I’m going to create a new Python file, call it urls.py. Okay, so there it is. Now, is that enough? Let’s try restarting our server.

04:35 Okay, so we get another error message, yippee! It’s telling us, If you see valid patterns in the file... blah, blah, blah, blah, blah,

04:50 ImproperlyConfigured... blah, blah, blah. django.core.exceptions..., blah, blah, blah. ...included URLconf 'module 'projects.urls'—okay, great!

04:58 So, as you see, it’s finding it, right? So what we created was good. Also, when you see a new friend, a new error message, that’s always a good sign because it tells you that what you did actually had an effect. So it’s telling us, “Okay, this ...does not appear to have any patterns in it.” Okay, so, The included URLconf, which is a success message telling us we managed to include the right thing, but currently, it ...does not appear to have any patterns in it.

05:28 If you see valid patterns in the file then the issue is probably caused by a circular import. We don’t really know what’s a circular import, but we can even see that Django gives us some tips here. However, that’s not the case for us right now.

05:39 There are no valid patterns in the file because this file, as of now, is completely empty. So, this is what applies. It doesn’t have any patterns in it. Let’s go ahead and fix that.

05:51 The simplest way of doing that is just heading over to this one. We know this one was working, so I’m just going to take what’s here and move it over to our new file.

06:05 Now, we’re not going to deal with this 'admin/', so I’m just going to get rid of it. And we don’t—actually, let me keep that. We don’t want to include, we don’t want to point forward to a different urls file—that’s what we did here—so I get rid of that.

06:22 And now maybe we can model something similar to what we were doing here.

06:29 What I want to say is if I get redirected to here, to this urls file, without anything else—you know, it’s just projects/ and then nothing else in the URL—then what I want to do is I want to point forward to somewhere else.

06:45 I want to point forward to where the logic happens in our Django app, which is going to be the views file.

06:55 Just cleaning up. I want to point into views and then project_list. That’s what I’m eventually going to want to create. Okay. So, here we have another error message that tells us Unresolved reference 'views'. That is because if we want to use the views inside of any app, we’re going to have to import it.

07:21 So I’ll say from projects import views.

07:27 Okay, so now this error’s gone, and PyCharm is already telling us something else, but let’s ignore this for now and restart our server…

07:42 to run into a new error! Ta-da! We’re going to deal with this error in the next video.

Avatar image for Martin Breuss

Martin Breuss RP Team on Aug. 12, 2020

Hello @eddhyne! But they really are friendly 🤗

While I don’t exactly know what’s happening here, I would look closer at this line:

ModuleNotFoundError: No module named ‘projects.urls’

This sounds to me like like the file you were working on, urls.py in your projects app might not have been saved correctly and the project overall is not aware of its existence.

What happens when you save your file and restart the development server?

Avatar image for Jacques Canuel

Jacques Canuel on Dec. 4, 2020

Hello Martin,

After modification of portfolio\settings.py and urls.py as of your instructions, I get the message “ModuleNotFoundError: No module named ‘projects.urls’” followed by “OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ‘<frozen importlib._bootstrap>’”, and the server stops!

Everything worked fine before those changes.

Thank you

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 4, 2020

Hi @Jacques! Check back through the video starting from here, this error is expected and explained in the tutorial. :)

If that still doesn’t solve it, here is a more general suggestion on where you can check, but I can only say more if you share the code of your project. That said, double-check:

Project & Files: You have a Django app called projects (note the plural!) in your Django project, and that it contains a file called urls.py (again, double-check the spelling)

Hope that helps and solve it, otherwise please specify in more detail what you did and tried and share your code

Avatar image for Jacques Canuel

Jacques Canuel on Dec. 6, 2020

Thanks Martin,

After double-checking my code and finishing the lesson, everything worked fine

: )

Avatar image for Stephen Smith

Stephen Smith on Dec. 29, 2020

Hi.

I’m trying to follow along, and am up to the ‘Build your routes’ video.

I’ve tried both django 2 and 3.

Yet when I create urls.py in projects I still get this error message. And urls.py does exist in portfolio.

ModuleNotFoundError: No module named 'portfolio.urls.py'; 'portfolio.urls' is not a package

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 30, 2020

Hi @Stephen J Smith, can you double-check how you’re trying to include your urls.py file from your portfolio app?

It should look like this:

path('portfolio/', include('portfolio.urls'))

Your error message makes me think that you might have added the file extension and wrote instead include('portfolio.urls.py')).

Let me know in case that solves it, otherwise please link your GitHub account or post your code to make it easier to find the bug :)

Avatar image for Stephen Smith

Stephen Smith on Dec. 30, 2020

Hi, I think you might be correct,not sure what the problem was. After a day of debugging, I deleted everything and started again. Now everything is as it should be. Cheers.

Avatar image for gregorysaison

gregorysaison on Jan. 21, 2021

Good video Martin!

But I have a question for you. In the portfolio\urls.py, we have the include thing.

But why we are using the ‘include’ thing? Can we make the same that the first line admin and wrote path('projects/', projects.urls),? The include is always necessary? What is the goal of the include compared to the first line admin?

Thanks!

Avatar image for mandar-gite

mandar-gite on Sept. 9, 2022

Hi Martin,

I have python3 installed as well anaconda in Ubuntu . It seems that there is some issue with interpreter picked up by pycharm . The folder in which i have created venv is placed in another folder called python (/projects/python/django-portfolio) which has other project folders and those use anaconda for managing the libraries installation etc. Is my guess correct ? if yes , how do i address this error?

The error messages are as follows:

(.env) (base) mandar@desk:~/projects/python/django-portfolio$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/urls/resolvers.py", line 717, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mandar/anaconda3/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/home/mandar/anaconda3/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/management/base.py", line 475, in check
    all_issues = checks.run_checks(
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/urls/resolvers.py", line 495, in check
    messages.extend(check_resolver(pattern))
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/urls/resolvers.py", line 494, in check
    for pattern in self.url_patterns:
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/mandar/projects/python/django-portfolio/.env/lib/python3.9/site-packages/django/urls/resolvers.py", line 725, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'projects.urls' from '/home/mandar/projects/python/django-portfolio/projects/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
Avatar image for Pythagoras

Pythagoras on April 14, 2023

Hi Martin!

For some reason I’m getting a different error message

File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'Projects'

Instead of projects.url it can’t find the Projects module

Become a Member to join the conversation.