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),
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)
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.