python – For creating app to upload and dislpaly images i am getting, ModuleNotFoundError: No module named ‘posts.url’

(.venv) > django-admin startproject django_project .
(.venv) > python manage.py startapp posts
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "posts",  # new
]
python manage.py migrate
from django.db import models


class Post(models.Model):
    title = models.TextField()
    cover = models.ImageField(upload_to='images/')

    def __str__(self):
        return self.title
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

(.venv) $ mkdir media (.venv) $ mkdir media/images

from django.contrib import admin

from .models import Post

admin.site.register(Post)
python manage.py makemigrations

Migrations for ‘posts’: posts/migrations/0001_initial.py – Create model Post

python manage.py migrate

Operations to perform: Apply all migrations: admin, auth, contenttypes, posts, session s Running migrations: Applying posts.0001_initial… OK

(.venv) > python manage.py createsuperuser

(.venv) > python manage.py runserver
from django.contrib import admin
from django.conf import settings  # new
from django.urls import path, include  # new
from django.conf.urls.static import static  # new

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("posts.urls")), # new
]

if settings.DEBUG:  # new
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path

from .views import HomePageView

urlpatterns = [
    path("", HomePageView.as_view(), name="home"),
]
from django.views.generic import ListView
from .models import Post


class HomePageView(ListView):
    model = Post
    template_name = "home.html"

(.venv) > mkdir templates
TEMPLATES = [
    {
        ...
        "DIRS": [BASE_DIR / "templates"],  # new
        ...
    },
]
<h1>Django Image Uploading</h1>
<ul>
  {% for post in object_list %}
    <h2>{{ post.title }}</h2>
    <img src="{{ post.cover.url}}" alt="{{ post.title }}">
  {% endfor %}
</ul>
**Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:UsersdivyaAppDataLocalProgramsPythonPython310libthreading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:UsersdivyaAppDataLocalProgramsPythonPython310libthreading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangocoremanagementcommandsrunserver.py", line 134, in inner_run      
    self.check(display_num_errors=True)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangocoremanagementbase.py", line 487, in check
    all_issues = checks.run_checks(
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangocorechecksregistry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangocorechecksurls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangocorechecksurls.py", line 24, in check_resolver
    return check_method()
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangourlsresolvers.py", line 480, in check
    for pattern in self.url_patterns:
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangoutilsfunctional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangourlsresolvers.py", line 696, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangoutilsfunctional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangourlsresolvers.py", line 689, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:UsersdivyaAppDataLocalProgramsPythonPython310libimportlib__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 "C:UsersdivyaDesktopprojectshelloworlddjangodjango_projecturls.py", line 23, in <module>
    path("", include("posts.urls")), # new
  File "C:UsersdivyaDesktopprojectshelloworlddjango.venvlibsite-packagesdjangourlsconf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "C:UsersdivyaAppDataLocalProgramsPythonPython310libimportlib__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 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'posts.urls'**

Leave a Comment