(urls.E004). Your URL Pattern is invalid in project level urls.py file

Question:

I am newbie to Django. I am getting the below error in-spite of rightly mapping the app level urls.py file to project level urls.py. Please help.

projectlevelurls.py

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

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

applevelurls.py

from importlib.resources import path
from xml.etree.ElementInclude import include
from . import views

urlpatterns = [
path('app_space/', views.home_page)
]

Error:

ERRORS:
?: (urls.E004) Your URL pattern <contextlib._GeneratorContextManager 
object at 0x0000024F8075D548> is invalid. Ensure that urlpatterns is 
a list of path() and/or re_path() instances.

Answers:

You should import path and include from django.urls in your app’s urls.py so:

from django.urls import path, include
from . import views
urlpatterns = [ 
    path('', views.home_page) 
]
Answered By: Sunderam Dubey

put this in MainProjectFolder(where settings.py exists)/urls:

path('', include ('app_space.urls'))

and inside your app_space/urls.py you can but a custom url like app_space/

also make sure you import include and path

from django.urls import path, include

also add this line to settings.py/INSTALLED_APP -> ‘app_space’,

app_space -> your app name

Answered By: Hashem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.