简体   繁体   中英

Django HTML template database query doesn't show anything

I'm trying to query some data and insert it into my HTML template.

My model looks like this

class Usertasks(models.Model):
    TaskID = models.CharField(max_length=40)
    user = models.CharField(max_length=40)
    TaskStatus = models.CharField(max_length=10)

And I have a view that has the following content

from callgolem.models import Usertasks

def dashboard(request):
    query_results = Usertasks.objects.all()
    if not request.user.is_authenticated:
        return redirect('/account/login/')
return render(request, 'dashboard.html')

and in my html template I wrote this

 <div class="tasks-list">
    <div>
      {% for item in query_results %}

          <p>{{ item.user }}</p>
          <p>{{ item.TaskID }}</p>
          <p>{{ item.TaskStatus }}</p>
      {% endfor %}
    </div>
 </div>

However when i'm loading the webpage the query is blank. Nothing is shown. I opened my DBviewer and made sure that there's actual data in what im trying to query.

def dashboard(request):
    query_results = Usertasks.objects.all()
    if not request.user.is_authenticated:
        return redirect('/account/login/')
    return render(request, 'dashboard.html',{'query_results':query_results})

change your view to this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM