Seeker90
Seeker90

Reputation: 895

How to display the contents of a table as a table in django?

I have made a simple django app. In my models.py I have defined a table-

class events(models.Model):
    id_campaign = models.CharField(default='newevent',max_length=100)
    status = models.CharField(default='100',max_length=100)
    name = models.CharField(default='100',max_length=100)

This is my views.py -

from django.shortcuts import render

# Create your views here.

from django.views.generic import TemplateView

from django.shortcuts import render
from website.models import *
from website.models import events


from django.views.generic import ListView

class HomePageView(TemplateView):
    template_name = 'base.html'


class AboutPageView(TemplateView):
    template_name = 'about.html'

class ContactPageView(TemplateView):
    template_name = 'contact.html'

class Success(TemplateView):
    template_name = 'success.html'
    def MyView(self, request):
        query_results = events.objects.all()
        return render(request, self.template_name)

And this is the success.html -

{% if user.is_authenticated %}
<header>
    <a href=" {% url 'logout'%}">Logout</a>
</header>

{% block content %}
<h2>you are logged in successfully. this is your personal space.</h2>

<table>
    <tr>
        <th>id_campaign</th>
        <th>status</th>
        <th>name</th>
   </tr>
    {% for item in query_results %}
    <tr>
        <td>{{ item.id_campaign }}</td>
        <td>{{ item.status }}</td>
        <td>{{ item.name }}</td>
    </tr>
    {% endfor %}
</table>

{% endblock %}

{% else %}

<h2>you are not logged in.</h2>

{%endif %}

My issue is that once the user logs in he is only able to see the hardcoded column names but the column values are not visible. It is as if the {% for item in query_results %}. part is not working.

What should I do? I want to view the contents in a tabular way?

Also I need to check the username and based on the user I need to show results, how do I do that?

if request.user.is_authenticated(): 
  username = request.user.username

if username is 'xyz' then I want to display the values from query_results and if username is 'abc' then I don't want to display.

P.S. - I am absolutely new to django

Upvotes: 2

Views: 797

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You've misunderstood how class-based views work. Your MyView method is never called (although note that even if it was, it still doesn't actually do anything to send query_results to the template).

You need to define the get_context_data method, which returns a dictionary:

class Success(TemplateView):
    template_name = 'success.html'
    def get_context_data(self, *args, **kwargs):
        query_results = events.objects.all()
        return {'query_results': query_results}

But note that a much better way is to use the appropriate generic view; you want to display a list of items, so use a ListView.

class Success(ListView):
    template_name = 'success.html'
    model = events

and in your template do {% for item in events_list %}.

Upvotes: 1

Astik Anand
Astik Anand

Reputation: 13047

You are using TemplateView in wrong way. You need to override the get_context_data() method in your TemplateView.

class Success(TemplateView):
    template_name = 'success.html'

    def get_context_data(self, **kwargs):
        context = super(Success, self).get_context_data(**kwargs)
        query_results = events.objects.all()
        context.update({'query_results': query_results})
        return context

It should work fine now.

Upvotes: 1

Related Questions