Home > Enterprise >  Django cannot response json data
Django cannot response json data

Time:03-16

I create device function in views.py for return json data like this.

views.py

def device(request):
    
    responseData = {
        'id': 4,
        'name': 'Test Response',
        'roles' : ['Admin','User']
    }

    return JsonResponse(responseData)

I set url path to views.device() in urls.py like this.

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('device/', views.device()),
]

when I save project it show error like this. How to fix it?

  File "C:\Users\MAX\Django\test_project\test\test\urls.py", line 23, in <module>
    path('device/', views.device()),
TypeError: device() missing 1 required positional argument: 'request'

CodePudding user response:

You are calling your device function when your urls are loaded without the request.

Your urls just need to know what function to call.

Instead of having path('device/', views.device()), it should be path('device/', views.device),

https://docs.djangoproject.com/en/4.0/topics/http/urls/#example

  • Related