Where does my "urls.py" file go in my Django project?

Question:

I’m using Django and Python 3.7. I used PyCharm to create a project and it created the following directory structure (some of the custom additions are my own):

mainpage
    __init__.py
    __pycache__
    admin.py
    apps.py
    fixtures
    management
    migrations
    models.py
    services.py
    templates
        mainpage
            trending.html
    tests.py
    urls.py
    views.py
mainpage_project
    __init__.py
    __pycache__
    settings
    wsgi.py
manage.py
templates
venv

The file "urls.py" looks like the below

from django.contrib import admin
from django.urls import path

from . import views

urlpatterns = [
    path('trending', trending, name='trending'),
]

I’m confused about where "urls.py" should go and what it should contain. When I start up my server, it gives me the following error

ModuleNotFoundError: No module named ‘mainpage_project.urls’

(venv) localhost:mainpage_project davea$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103096950>
Traceback (most recent call last):
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mainpage_project.urls'

Where should my "urls.py" file go and what should it contain?

Asked By: Dave

||

Answers:

You are missing urls.py in your project mainpage_project. i.e,

mainpage_project
    __init__.py
    __pycache__
    settings
    wsgi.py
    urls.py    # Here

In the words of Vitor Freitas, A Complete Beginner’s Guide to Django – Part 1:

urls.py is responsible for mapping the routes and paths in your project. For example, if you want to show something in the URL /trending/, you have to map it here first.

From your directory structure mainpage_project is your project, mainpage is your app.

Each app can have its own urls.py. Django actually checks for it inside your project folder.

Typical urls.py looks like:

from django.conf.urls import url
from django.contrib import admin
from django.urls import path

from mainpage import views

urlpatterns = [
    path('trending', trending, name='trending'),
]

From comments, you are getting an import error when adding urls.py because inside your urls.py you are trying to import views from mainpage_project‘s folder.

from . import views

. notation refers to the current directory from where it is called, i.e., the mainpage_project directory.

And there isn’t any views.py inside the mainpage_project directory. But views.py is present inside the mainpage directory. So update it as:

from mainpage import views