Multiple Templates in single list view

84 kali dilihat
Langsung ke pesan pertama yang belum dibaca

Trippy Samurai

belum dibaca,
21 Nov 2021, 04.39.3921/11/21
kepadaDjango 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

belum dibaca,
21 Nov 2021, 23.19.5621/11/21
kepadaDjango users
Any one plz

Elena Williams

belum dibaca,
21 Nov 2021, 23.34.1421/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 01.49.0222/11/21
kepadaDjango 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

belum dibaca,
22 Nov 2021, 02.12.5922/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 02.42.0322/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 02.43.3622/11/21
kepadaDjango 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

belum dibaca,
22 Nov 2021, 02.52.4722/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 06.05.3222/11/21
kepadaDjango 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

belum dibaca,
22 Nov 2021, 07.54.3422/11/21
kepadaDjango users
Anyone plz review my code i am struggling to figure out what the issue here is

ram s

belum dibaca,
22 Nov 2021, 10.48.1122/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 10.48.1222/11/21
kepadadjango...@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

belum dibaca,
22 Nov 2021, 10.50.1622/11/21
kepadaDjango users
Its done bro model is configured already i didn't just posted the models.py file

Trippy Samurai

belum dibaca,
22 Nov 2021, 10.51.3022/11/21
kepadaDjango users
Screenshot 2021-11-22 at 9.21.06 PM.png

Lalit Suthar

belum dibaca,
24 Nov 2021, 12.33.3624/11/21
kepadadjango...@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

belum dibaca,
24 Nov 2021, 12.34.3424/11/21
kepadadjango...@googlegroups.com
I have gone through your views and template. They are looking fine to me. Are you getting any error?
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru