Home > Software design >  Unable to get the object in my html template
Unable to get the object in my html template

Time:03-24

I've created objects in the admin page of my app, but I'm unable to call the object in my html template. I will put the views and the html lines below

from django.shortcuts import render, redirect
from .models import *
from .forms import *

def index(request):
    tasks = Task.objects.all()

    form = TaskForm()

    if request.method == 'POST':
        form = TaskForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/')
        
    context = {'tasks': tasks, 'form': form}
    return render(request, 'todo_app/list.html')

 {% for task in tasks %}
            <div >
               <p>{{task}}</p>
            </div>
        {% endfor %} 

CodePudding user response:

You forgot to send context to template:

Optional arguments

context

A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.

    context = {'tasks': tasks, 'form': form}
    return render(request, 'todo_app/list.html', context)
  • Related