-1
Following the tutorial for creating API with DRF, I encountered difficulties while mapping the urls. By running 'python manage.py runserver'
, I get the following error path('', include(loja.urls)), NameError: name 'loja' is not defined
. I can’t see where I’m failing in this process and hope I can count on help.
project structure:
sistema/
loja/
urls.py
views.py
sistema/
urls.py
py views.
@csrf_exempt
def store_list(request):
if request.method == 'GET':
store= Lojas.objects.all()
serializer = LojasSerializer(store, many=True)
return JsonResponse(serializer.data, safe=False)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = LojasSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
shop/urls.py
from django.urls import path
from loja import views
urlpatterns = [
path('loja/', views.store_list),
]
system/urls.py
from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from loja import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(loja.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
output error:
path('', include(loja.urls)), NameError: name 'loja' is not defined
EDIT: After output error, another error message appears and my operation is automatically terminated.
error:
SError: [WinError 123] A sintaxe do nome do arquivo, do nome do diretório ou do rótulo do volume está incorreta: '<frozen importlib._bootstrap>'
I believe I followed the tutorial carefully, so I can’t understand where I’m going wrong.
If you see the documentation about url namespaces, you have to pass as a string:
path('', include('loja.urls'))
– afonso
@drec4s I tried to pass as string. Unfortunately the result was the same.
– zangs
So the problem is with the module name. It is
loja
orlojas
? Another thing, you added this app toINSTALLED_APPS
in the Settings?– afonso
In case it would be store same. Yes, I added in INSTALLED_APPS, but persists the same error :(
– zangs