Home > front end >  where does django REST API inherits 'users' from?
where does django REST API inherits 'users' from?

Time:06-24

I've created an API with certain paraments and fed it to my django URL with the following sample..

def home(request):
    response = requests.get('http://127.0.0.1:8000/api/workers/')
    workers = response.json()
    return render(request, "home.html", {'users': workers})
    pass

Where does it inherit the 'users' from?

my API doesn't contain anything related to it and I would like to add a second API ontop of such as :

return render(request, "home.html", {'users': workers}, {'users': speciality})

will not work because users is already inherited.

CodePudding user response:

It seems like the problem is that you are setting the context for “users”, twice. When you set context, think of it like “nicknaming”. You first give workers, the nickname users. And then it seems as though you’re also trying to give specialty, the nickname users.

They can’t both have the same name, or else one of them will be overwritten. Let’s break down what’s happening..

Original code:

  workers = response.json()
  # Context: refer to workers as “users” in the template. 
  return render(request, "home.html", {'users': workers})

Desired code:

# Context: refer to works as “users” in the template. Also refer to speciality as “users in the template.
return render(request, "home.html", {'users': workers}, {'users': speciality})


But also, check out Django documentation on render():


render(request, template_name, context=None, content_type=None, status=None, using=None)


You can’t define context in two separate dictionaries like in this example:


return render(request, "home.html", {'users': workers}, {'users': speciality})


Short Answer:

  1. workers and speciality need to have different names.
  2. workers and speciality need to be defined in a single context dictionary.

Maybe try something like this:


return render(request, template_name="home.html", context={'users': workers, 'user_specialities': speciality})


  • Related