Routing with multiple applications failing

hi,
I have followed the Bezcoder DRF / CRUD tutorial with the informations from the “Django for APis” book of William Vincent.
While I have 1 project containing 1 app : it’s ok. I can curl the API fine.

The problem came after I’ve added a 2nd application: an errr is coming because I think Django can’t know which view belongs to which app: app1 or app2

api/urls.py line 9 in …
url(r’^app1$’, views.app1_list),
AttributeError : module ‘App2.views’ has no attribute ‘app1_list’

from django.urls import path
from django.conf.urls import url
from .views import  app1APIView, app2APIView
from app1 import views
from app2 import views

urlpatterns = [
    path('', app2APIView.as_view()),
    url(r'^app1$', views.app1_list),
    url(r'^app1/(?P<pk>[0-9]+)$', views.app1_detail),
    url(r'^app1/enabled$', views.app1_list_enabled),
    url(r'^app2$', views.app2_list),
    url(r'^app2/(?P<pk>[0-9]+)$', views.app2_detail),
    url(r'^app2/enabled$', views.app2_list_enabled)

This code is working only if I comment app1 or app2 but not both

You’ve got a couple different ways of handling this.

My recommendation would be to just import app1.views and app2.views, and prefix your urls with the corresponding app name:
url('^app1$', app1.views.app1_list),

Thanks @KenWhitesell for you answer.
However, doing

from django.urls import path
from django.conf.urls import url

from .views import app1APIView, app2APIView

from app1 import views
from app2 import views

urlpatterns = [
#path(’’, app2APIView.as_view()),
url(r’^app1$’, app1.views.app1_list),
url(r’^app1/(?P[0-9]+)$’, app1.views.app1_detail),
url(r’^app1/enabled$’, app1.views.app1_list_enabled),
url(r’^app2$’, app1.views.app2_list),
url(r’^app2/(?P[0-9]+)$’, app1.views.app2_detail),
url(r’^app2/enabled$’, app1.views.app2_list_enabled)

ends with error " app1 is not defined"
while commenting all the app2 references and not prefixing app1 but just :

urlpatterns = [
#path(’’, app2APIView.as_view()),
url(r’^app1$’, views.app1_list),
url(r’^app1/(?P[0-9]+)$’, views.app1_detail),
url(r’^app1/enabled$’, views.app1_list_enabled),
#url(r’^app2$’, app1.views.app2_list),
#url(r’^app2/(?P[0-9]+)$’, app1.views.app2_detail),
#url(r’^app2/enabled$’, app1.views.app2_list_enabled)

is ok.

I think you overlooked this part of my answer:

OK but if I do that

“url is not defined”

arizes which is normal.

My fault, my previous reply wasn’t clear. You still need your other imports. What you need to change is the from app1 import views to import app1.views (and the same for app2)

@KenWhitesell no fault is in play when it’s about helping.

And by the way, this solves the issue.
thanks I have been searching in many (wrong it seems) places for this.

Tell me if I have to change the title or something to mark it as solved.

There should be an icon underneath each reply that you can click to select the solution. Find the reply you wish to mark, and click the “solution” box.