Multiple Templates in single list view

84 views
Skip to first unread message

Trippy Samurai

unread,
Nov 21, 2021, 4:39:39 AM11/21/21
to Django users
Hello,
I have different views for displaying different templates how do i write them into one single Listview so that i can take care of DRY


Screenshot 2021-11-21 at 3.08.58 PM.png

Trippy Samurai

unread,
Nov 21, 2021, 11:19:56 PM11/21/21
to Django users
Any one plz

Elena Williams

unread,
Nov 21, 2021, 11:34:14 PM11/21/21
to django...@googlegroups.com
Hi,

The problem is it's unclear what your question is. Can you be clearer about what outcome you're trying to achieve? 


---
Elena Williams
Github: elena


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d86e1449-a17e-4e93-9dd2-1fae9a7e91d7n%40googlegroups.com.

Trippy Samurai

unread,
Nov 22, 2021, 1:49:02 AM11/22/21
to Django users
Hi Elena Thanks for the reply i have three different html pages to display open tickets closed tickets accepted tickets etc each of them have different ticket statuses i have them written in different views as above to display each type of ticket at different pages how can i combine all three in  a single view so that i dont repeat my logic in views.Is there a way to acheive all the three views writing in a single view?

Lalit Suthar

unread,
Nov 22, 2021, 2:12:59 AM11/22/21
to django...@googlegroups.com
we can go like 

```

class Manager(...):
def get_context_data(self, **kwargs):
context = super() ....
context["open_tickets"] = Ticket.objects.filter(status="OPEN")
context["accepted_tickets"] = Ticket.objects.filter(status="ACCEPTED")
context["completed_tickets"] = Ticket.objects.filter(status="COMPLETED")
return context
```

David Nugent

unread,
Nov 22, 2021, 2:42:03 AM11/22/21
to django...@googlegroups.com
Hi Lalit,

On Mon, Nov 22, 2021 at 6:12 PM Lalit Suthar <sutharl...@gmail.com> wrote:
we can go like 

```

class Manager(...):
def get_context_data(self, **kwargs):
context = super() ....
context["open_tickets"] = Ticket.objects.filter(status="OPEN")
context["accepted_tickets"] = Ticket.objects.filter(status="ACCEPTED")
context["completed_tickets"] = Ticket.objects.filter(status="COMPLETED")
return context
```

That is certainly one value solution, though for any call there are potentially (ignoring cache) 3 db queries.

To avoid that, an arg or kwarg could be passed to the view by implementing `get` and allowing for it in the associated path in urls.py. This is typically a better pattern and very similar to what Dango admin does when a filter is enabled.

I would also avoid the word `Manager` in a CBV, since `Manager` has a special meaning in Django.

Regards, David

Trippy Samurai

unread,
Nov 22, 2021, 2:43:36 AM11/22/21
to Django users
What about the templates i have three different templates for as u can see in template_name in the above views ,how do i deal with it if i actualyl had one template the above could have worked.

David Nugent

unread,
Nov 22, 2021, 2:52:47 AM11/22/21
to django...@googlegroups.com
Well, there are several ways you can deal with that.

Probably easiest is to override get_template() and switch the template based on whatever type of ticket you're managing or you can change the value of self.template_name based on the request.

Or maybe you prefer a single template to deal with all cases with conditional blocks where appropriate.


Regards,
David

Trippy Samurai

unread,
Nov 22, 2021, 6:05:32 AM11/22/21
to Django users
Thanks David for the idea i appreciate it i have gone with last way of doing things but it doesnt render anything on data

Pls correct my code below 



views.py:

class DeveloperTicketView(TemplateView):
template_name = 'app/ticket_view.html'
ticket = Ticket.objects.all()
extra_content = {'ticket_type':ticket}

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['open_tickets'] = Ticket.objects.filter(status = 'Opened')
context['accepted_tickets'] = Ticket.objects.filter(status = 'Accepted',accepted_by = self.request.user)
context['completed_tickets'] = Ticket.objects.filter(status = 'Completed',accepted_by = self.request.user)
context['closed_tickets'] = Ticket.objects.filter(status = 'Closed',accepted_by = self.request.user)
return context



ticket_view.html

{% extends 'app/base.html' %}

{% block body %}
{% for tickets in ticket_type%}
{% if tickets.status == "Opened" %}
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Status</th>
<th>Created</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for ticket in open_tickets %}
<tr>
<td><a href="">{{ ticket.id }}</a></td>
<td>{{ ticket.status }}</td>
<td>{{ ticket.created_by }}</td>
<td>{{ ticket.ticket_title }}</td>
<td>{{ ticket.ticket_description }}</td>
<td><a href="{% url 'accept_tickets' pk=ticket.id %}"> Accept</a>
</tr>
{% endfor %}
</tbody></table>
{% elif tickets.status == 'Accepted' %}
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Status</th>
<th>Created by</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>

<tbody>
{% for ticket in accepted_tickets %}
<tr>
<td><a href="">{{ ticket.id }}</a></td>
<td>{{ ticket.status }}</td>
<td>{{ ticket.created_by }}</td>
<td>{{ ticket.ticket_title }}</td>
<td>{{ ticket.ticket_description }}</td>
<td><a href="{% url 'mark_complete' pk=ticket.id %}">Complete</a>
</tr>
{% endfor %}
</tbody></table>

{% elif tickets.status == 'Completed' %}

<table class="table table-bordered">

<thead>
<tr>
<th>ID</th>
<th>Status</th>
<th>Created by</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>

<tbody>
{% for ticket in completed_tickets %}
<tr>
<td><a href="">{{ ticket.id }}</a></td>
<td>{{ ticket.status }}</td>
<td>{{ ticket.created_by }}</td>
<td>{{ ticket.ticket_title }}</td>
<td>{{ ticket.ticket_description }}</td>
</tr>
{% endfor %}
{% else %}
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Status</th>
<th>Created by</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>

<tbody>
{% for ticket in closed_tickets %}
<tr>
<td><a href="">{{ ticket.id }}</a></td>
<td>{{ ticket.status }}</td>
<td>{{ ticket.created_by }}</td>
<td>{{ ticket.ticket_title }}</td>
<td>{{ ticket.ticket_description }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endblock %}

Trippy Samurai

unread,
Nov 22, 2021, 7:54:34 AM11/22/21
to Django users
Anyone plz review my code i am struggling to figure out what the issue here is

ram s

unread,
Nov 22, 2021, 10:48:11 AM11/22/21
to django...@googlegroups.com
i think u forgot to configure model, please once configure and complete check first

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

ram s

unread,
Nov 22, 2021, 10:48:12 AM11/22/21
to django...@googlegroups.com
will update asap

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Trippy Samurai

unread,
Nov 22, 2021, 10:50:16 AM11/22/21
to Django users
Its done bro model is configured already i didn't just posted the models.py file

Trippy Samurai

unread,
Nov 22, 2021, 10:51:30 AM11/22/21
to Django users
Screenshot 2021-11-22 at 9.21.06 PM.png

Lalit Suthar

unread,
Nov 24, 2021, 12:33:36 PM11/24/21
to django...@googlegroups.com
> Anyone plz review my code i am struggling to figure out what the issue here is

What is the issue you are trying to resolve right now?

Lalit Suthar

unread,
Nov 24, 2021, 12:34:34 PM11/24/21
to django...@googlegroups.com
I have gone through your views and template. They are looking fine to me. Are you getting any error?
Reply all
Reply to author
Forward
0 new messages