django get context data code example

Example 1: retrieve object from context in django

def index (request):
  context={}
  data= Data.objects.all()
  filesdata = 
  context["info"] = data 
  if request.method == 'POST':
    errorAndTime = addFile(request)
    context["error"] = errorAndTime["error"]
    context["time"] = errorAndTime["time"]
    # error = errorAndTime["error"]
    # time = errorAndTime["time"]
    return render(request, "dataAnalysis/index.html", context=context)
  #When you retrieve your data just call the name of the context u don't need 
  #to call it like this context.info or something like that 
      {% for data in info %}
            <tr>
              <td>{{ data.file_id }}</td>
              <td>{{ data.name }}</td>
              <td>{{ data.age }}</td>
              <td>{{ data.email }}</td>
              <td>{{ data.phone }}</td>
              <td>{{ data.gender }}</td>
            </tr>
      {% endfor %}

Example 2: get context data django

class PublisherDetail(DetailView):

    model = Publisher

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['book_list'] = Book.objects.all()
        return context