No module named 'urls' in Django

Asked

Viewed 1,353 times

0

I’m studying about URL division in Django and I can’t direct my main url file to high school.

Tracking:

Traceback (most recent call last):
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\management\base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
    return check_method()
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\urls\resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\PASTAD~1\MOOC\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\PASTA DO CRISTHIAN\MOOC\Scripts\simplemooc\simplemooc\urls.py", line 22, in <module>
    url(r'^', include('urls')),
  File "C:\PASTAD~1\MOOC\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\PASTAD~1\MOOC\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked

Main file of Urls:

from django.conf.urls import url, include
from django.contrib import admin
from simplemooc.core import views
from simplemooc.core import urls

urlpatterns = [
    url(r'^', include('urls')),
    url(r'^admin/', admin.site.urls),
]

Secondary URL file:

from django.conf.urls import url
from simplemooc.core import views   

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^contato/$', views.contact, name='contact'),
]

Error:

ModuleNotFoundError: No module named 'urls'

Could help?

Thank you.

2 answers

1

By the message, your module urls can not be imported for some reason, let’s create a minimalist project to see how it works:

Create a Django project in a temporary directory:

$ django-admin startproject hello  

Go to the project directory and create an app called core:

$ cd hello
$ django-admin startproject hello

Edit the file hello/settings.py and, in the section INSTALLED_APPS add the app created, and it should look like this:

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

Edit the urls.py file in the project directory (hello/urls.py) to get the following content:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^core/', include('core.urls', namespace='core', app_name='core'))
]

create a directory for core templates:

$ mkdir -p core/templates/core

create a core template called hello.html, with the content

<p>Hello World</p>

Create (or edit), in the reaiz of the core app, the file urls.py with the content

from django.conf.urls import url
from core .views import hello

urlpatterns = [
    url(r'^hello/$', hello,  name='hello'),
]

Create your views (views.py) in the app directory (core) with the content:

from django.shortcuts import render
def hello(request):
    return render(request, 'core/hello.html')

Finally run the project, point the browser to http://127.0.0.1:8000/core/hello/ and see Hello World.

0

Instead of doing it that way:

from django.conf.urls import url, include
from django.contrib import admin
from simplemooc.core import views
from simplemooc.core import urls

urlpatterns = [
    url(r'^', include('urls')),
    url(r'^admin/', admin.site.urls),
]

Remove the quotes and knife like this:

from django.conf.urls import url, include
from django.contrib import admin
from simplemooc.core import views
from simplemooc.core import urls

urlpatterns = [
    url(r'^', include(urls)),
    url(r'^admin/', admin.site.urls),
]

Why once you’ve imported them You can pass them without quotation marks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.