Views Django

Hi there,

I have a view function which is:

def my_func(request, dict):
    dict = {"2":"1"}
    return JsonResponse(dict)

but when I use this view, it says: "my_func() missing 1 required positional argument: ‘dict’ "

Why it says that “dict” is missing? I just want to return my dictionary to the html page, without passing the context. Any suggestions?

Thanks.

Hi khavro21,

I first want to point out that you shouldn’t name a variable dict because it overrides the built-in dict function. From now on, in code I’ll be renamingdict to my_dict.

The error my_func() missing 1 required positional argument: ‘dict’ is indicating that the signature of your method doesn’t match how you’re calling it. The function my_func asks for two arguments, request and my_dict. However your url is likely not passing any arguments to it, you can read more about that with the path function here.

You need to determine if you need the URL to pass an argument to the view or if the view needs nothing from the URL and can return the data. If you need the URL to pass an argument, you need to change the respective path(s) in your urls.py file to do so.

If you want the view to respond to that given URL but don’t need any arguments, dropping my_dict as an argument to the function will suffice:

def my_func(request):
    ...

Understood, will try it this way.
Thanks!