3

I'm using Django 2.0 and DRF 3.7.7(novice with both) and trying to add URL patterns in app_config/urls.py:

router = routers.DefaultRouter()
router.register(r'^submit_free_account', SubmitFreeAccount, 'SubmitFreeAccount')

app_name = 'app_config'  #the weird code

urlpatterns = [
    path('getSourcesNodes', GetSourcesNodes.post, name='GetSourcesNodes'),
    path('getAppsNodes', GetAppsNodes.post, name='GetAppsNodes'),
]

urlpatterns += router.urls

And in main urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('config/', include('app_config.urls', namespace='app_config')),
]

So when I trying to request config/submit_free_account I have 404 errors with strange URL patterns tried by Django:

config/ ^$ [name='api-root']
config/ ^\.(?P<format>[a-z0-9]+)/?$ [name='api-root']

How to add router URL patterns correctly in Django 2.0?

kukuruzo91
  • 111
  • 11
  • 1
    `router.registet` is supposed to be used with viewsets. Is SubmitFreeAccount a viewsets? Show the code. – Daniel Roseman Dec 30 '17 at 21:27
  • 1
    SubmitFreeAccount is APIView (supposed to be standard class for DRF views) – kukuruzo91 Dec 31 '17 at 10:09
  • You don't need a router for a view, only a viewset. Use a standard `path()` or `url()` to define the route. – Daniel Roseman Dec 31 '17 at 10:33
  • 1
    Daniel Roseman, but if this way I have an error: "__init__ takes 1 positional arguments but 2 were given". And it is DRF, so I don't want to have as_view() method. So I can't have CLASS based view. – kukuruzo91 Jan 01 '18 at 17:05
  • I don't understand why you don't want to use as_view. That is how you register a class based view. – Daniel Roseman Jan 01 '18 at 17:14
  • Daniel Roseman, I just thought that DRF APIViews don't support this method, but now I fixed some mistakes in my code and this has started to work. Thanks. Without your comment, I wouldn't do this. – kukuruzo91 Jan 02 '18 at 14:55

2 Answers2

0

see: Django REST Framework URLs with Django 2.0

router = routers.DefaultRouter()
# change to (see user8814926's answer):
router.register('submit_free_account', SubmitFreeAccount, 'SubmitFreeAccount')

app_name = 'app_config'  #the weird code

urlpatterns = [
    path('getSourcesNodes', GetSourcesNodes.post, name='GetSourcesNodes'),
    path('getAppsNodes', GetAppsNodes.post, name='GetAppsNodes'),

    #add this:
    path('', include(router.urls)),
]

so summarize it:

  • don't use the regex
  • include the routes not by adding to the list, but instead with the include method as shown above
Soraphis
  • 706
  • 7
  • 26
-2

Change

router.register(r'^submit_free_account', SubmitFreeAccount, 'SubmitFreeAccount')

to

router.register('submit_free_account', SubmitFreeAccount, 'SubmitFreeAccount')
neonua
  • 152
  • 2
  • 4
  • 1
    I tried this, also thought that this didn't suppose the regex expression, but the situation wasn't changed. – kukuruzo91 Dec 31 '17 at 10:16