URL files error

Hello I’m an 11 year old boy who looking to learn python and django. I have been trying this many times but I keep receiving this error it seems to be coming from this URL file

"""
URL configuration for demo project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path("myapp/", include("myapp.urls"))
]

myapp/home

This is the error is keep receiving:

Traceback (most recent call last):
  File "/Users/kevin/code/django/demo/myapp/urls.py", line 2, in <module>
    from . import views
ImportError: attempted relative import with no known parent package
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/demo/settings.py
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/demo/settings.py
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/myapp/urls.py
Traceback (most recent call last):
  File "/Users/kevin/code/django/demo/myapp/urls.py", line 2, in <module>
    from . import views
ImportError: attempted relative import with no known parent package
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/demo/urls.py
Traceback (most recent call last):
  File "/Users/kevin/code/django/demo/demo/urls.py", line 22, in <module>
    path('admin/', admin.site.urls),
  File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 251, in inner
    self._setup()
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 605, in _setup
    AdminSiteClass = import_string(apps.get_app_config("admin").default_site)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 156, in get_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 81, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 61, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/demo/settings.py
kevin@Jaspers-MBP-2016 django % /usr/local/opt/pytho
n@3.10/libexec/bin/python /Users/kevin/code/django/d
emo/demo/urls.py
Traceback (most recent call last):
  File "/Users/kevin/code/django/demo/demo/urls.py", line 22, in <module>
    path('admin/', admin.site.urls),
  File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 251, in inner
    self._setup()
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 605, in _setup
    AdminSiteClass = import_string(apps.get_app_config("admin").default_site)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 156, in get_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 81, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 61, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

If you want to see the full code it’s here GitHub - kevin2e754swap/Django-Cms

When I run python manage.py runserver 5000 from ~/code/Django-Cms/demo

python manage.py runserver 5000
Traceback (most recent call last):
  File "/Users/me/code/Django-Cms/demo/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

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

Traceback (most recent call last):
  File "/Users/me/code/Django-Cms/demo/manage.py", line 22, in <module>
    main()
  File "/Users/jasperfrumau/code/Django-Cms/demo/manage.py", line 13, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Once I have venv up in Visual Studio Code as shown here for example and run again I get

python manage.py runserver 5000
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 486, in check
    all_issues = checks.run_checks(
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/core/checks/urls.py", line 136, in check_custom_error_handlers
    handler = resolver.resolve_error_handler(status_code)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 732, in resolve_error_handler
    callback = getattr(self.urlconf_module, "handler%s" % view_type, None)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 711, in urlconf_module
    return import_module(self.urlconf_name)
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/user/code/Django-Cms/demo/demo/urls.py", line 23, in <module>
    path("myapp/", include("myapp.urls"))
  File "/Users/user/code/Django-Cms/demo/.venv/lib/python3.10/site-packages/django/urls/conf.py", line 39, in include
    urlconf_module = import_module(urlconf_module)
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/user/code/Django-Cms/demo/myapp/urls.py", line 5, in <module>
    path("home", views.home, name=home)
NameError: name 'home' is not defined

The error NameError: name 'home' is not defined in your myapp/urls.py file so that might mean that the string 'home' should be passed as the name argument instead of an undefined variable. VS Code also shows

"home" is not defined Pylance[reportUndefinedVariable]
(https://github.com/microsoft/pyright/blob/main/docs/configuration.md#
reportUndefinedVariable)

for home in

from django.urls import path
from . import views

urlpatterns = [
    path("home", views.home, name=home)
]

The home should made a string 'home with single quotes. But then you seems to have another undefined issue

NameError: name ‘myapp’ is not defined.

You may need something like

from django.contrib import admin
from django.urls import path, include  # include is necessary for including other app URLs

urlpatterns = [
    path('admin/', admin.site.urls),
    path('myapp/', include('myapp.urls')),  # Include the URL configuration from the myapp app
]

but you better check your demo for imports and actual paths to be added

Welcome @kevin2e754swap !

You do not run individual files in a Django project. Running your urls.py file isn’t supposed to work.

You don’t mention how you’re trying to learn Django, so I don’t know what source information you’re working from. But, I would suggest you start with either the Official Django Tutorial or the Django Girls Tutorial

3 Likes

hello sir…is there any way i can contact you privately?..if not i can just a few things about the issues i’m experiencing with my project.
So thing is i already complete my Student management system django webapp where i have columns for Matric number, First name, Last name, email, course of study and gpa. Also have a view, edit and delete icon button for every newly addded student.
So then i thought about adding some extra features on my own as i already finished builiding with a Youtube tutorial. So i wanted to add a photo ID feature to allow for image uploads for every student in a column before the first one which was the MATRIC NUMBER column. So i created a photo field in my app’s models.py and run migrations. Finally the photo column came up. But then i wanted to add an upload feature to my form so every student gets to upload a photo of themselves while uploading their records but this led to issues i havent been able to resolve ever since when i click the edit button it displays an error page

Welcome @Olufamous619 !

If you’re having a specific issue with which you would like assistance, please open a new topic for that discussion. Include a detailed description of the issue, along the specific code that you are having difficulties with. If you are getting any kind of error message, please include the complete error with the traceback from your runserver console.

Please do not post screenshots of code or error messages. Copy/paste the text into the body of your post, marked as “preformatted text”.