django return context 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"]
return render(request, "dataAnalysis/index.html", context=context)
{% 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):
context = super().get_context_data(**kwargs)
context['book_list'] = Book.objects.all()
return context