Hello I am working on adding a drop down selection to my filters so far I am not having any luck. I recently changed my views.py to use a class instead of a def for this. Now when I use filter.form it shows every field in my models. It should only show the following fields circuitid, bandwidth, region, carrier, status, segmentname, and mrcnew and each should be a dynamically generated drop down list from the respective model field only displaying distinct values. I tried creating my own form with a function that would return these values using choicefield but I can't get this form to come into the template. A point in the right direction would be much appreciated!
my views.py is below:
and my template looks like this:
-- class Circuitinfotable(models.Model): id1 = models.IntegerField(blank=True, null=True) pid = models.CharField(max_length=255, blank=True, null=True) circuitid = models.CharField(primary_key=True,max_length=255, blank=False, null=False) bandwidth = models.CharField(max_length=255, blank=True, null=True) region = models.CharField(max_length=255, blank=True, null=True) bw = models.IntegerField(blank=True, null=True) tableaupathname = models.CharField(max_length=255, blank=True, null=True) handoffalocaddress = models.CharField(max_length=255, blank=True, null=True) handoffaloccity = models.CharField(max_length=255, blank=True, null=True) handoffalocst = models.CharField(max_length=255, blank=True, null=True) alocationaddress = models.CharField(max_length=255, blank=True, null=True) alocationcity = models.CharField(max_length=255, blank=True, null=True) alocst = models.CharField(max_length=255, blank=True, null=True) handoffzlocaddress = models.CharField(max_length=255, blank=True, null=True) carrier = models.CharField(max_length=255, blank=True, null=True) handoffzloccity = models.CharField(max_length=255, blank=True, null=True) handoffzlocst = models.CharField(max_length=255, blank=True, null=True) zlocationaddress = models.CharField(max_length=255, blank=True, null=True) zlocationcity = models.CharField(max_length=255, blank=True, null=True) zlocst = models.CharField(max_length=255, blank=True, null=True) segmentid = models.CharField(max_length=255, blank=True, null=True) segmenttype = models.CharField(max_length=255, blank=True, null=True) status = models.CharField(max_length=255, blank=True, null=True) purpose = models.CharField(max_length=255, blank=True, null=True) installdate = models.DateField(blank=True, null=True) termdate = models.DateField(blank=True, null=True) termlengthmos = models.IntegerField(blank=True, null=True) legacy = models.CharField(max_length=255, blank=True, null=True) installciopsticket = models.CharField(max_length=255, blank=True, null=True) retermciopsticket = models.CharField(max_length=255, blank=True, null=True) discociopsticket = models.CharField(max_length=255, blank=True, null=True) notes = models.CharField(max_length=255, blank=True, null=True) kma = models.CharField(max_length=255, blank=True, null=True) group = models.IntegerField(blank=True, null=True) cktidkey = models.IntegerField(blank=True, null=True) visiolink = models.CharField(max_length=255, blank=True, null=True) latestjiraticket = models.CharField(max_length=255, blank=True, null=True) disconnectdate = models.DateField(blank=True, null=True) segmentname = models.CharField(max_length=255, blank=True, null=True) mrcnew = models.TextField(blank=True, null=True) # This field type is a guess. termyrs = models.IntegerField(blank=True, null=True) monthbudget = models.IntegerField(blank=True, null=True) monthdisco = models.IntegerField(blank=True, null=True) mrcold = models.TextField(blank=True, null=True) # This field type is a guess. kmz = models.CharField(max_length=255, blank=True, null=True)
def __str__(self): return self
class Meta: managed = False db_table = 'circuitinfotable'
My filter.py code is below:
from .models import Circuitinfotablefrom .forms import CircuitFormimport django_filters
class CircuitInfoFilter(django_filters.FilterSet.form): class Meta: model=Circuitinfotable #fields = ['circuitid', 'bandwidth', 'region', 'carrier', 'status', 'segmentname', 'mrcnew'] form=CircuitForm
My forms.py is below:
from django import formsfrom .models import Circuitinfotable
def getList(item_list, keyValue): valueList=[] for each in item_list: valueList.append(each[keyValue]) return valueList
class CircuitForm(forms.Form): class Meta: model = Circuitinfotable fields = ('circuitid', 'bandwidth', 'region', 'carrier', 'status', 'segmentname', 'mrcnew') region_dict=Circuitinfotable.objects.values('region').distinct('region')
def __init__(self, *args, **kwargs): super(CircuitForm, self).__init__(*args, **kwargs) self.fields['region'].queryset = forms.ChoiceField(getList(region_dict, 'region')) self.fields['bandwidth'].queryset = Bandwidth.objects.none() self.fields['circuitid'].queryset = Circuitid.objects.none() self.fields['carrier'].queryset = Carrier.objects.none() self.fields['status'].queryset = Status.objects.none() self.fields['segmentname'].queryset = Segmentname.objects.none() self.fields['mrcnew'].queryset = Mrcnew.objects.none()Enter code here...from django.shortcuts import renderfrom django.http import HttpResponse
from django.views.generic import TemplateView, ListView
from django.http import HttpResponseRedirect
from django_filters.views import FilterView
from .models import Circuitinfotable
from .filters import CircuitInfoFilter
from .forms import CircuitForm
import re
# Create your views here.
class search(FilterView):
model = Circuitinfotable
filter_class = CircuitInfoFilter
form_class = CircuitForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
and my template looks like this:
Enter code here...<html><head>
<meta charset="UTF-8">
<title>Circuits</title>
<style>
h1 {
color:blue;
}
h4 {
color:red;
}
.tabledata {
background: #395870; color:#fff;
}
.tablerow:nth-child(even) {
background-color: #f2f2f2;
}
.form{
width:100%;
}
.field {
background: #white; float: left; margin: 1%; width: 200;
}
</style>
{% extends 'ciopsNavbar.html' %}
{% block content %}
<div style="position: relative; top: 60px;">
<form method="get">
{{ filter.form.as_table }}
<button type="submit">Search</button>
<a href="/searchlit/customsearch/">
<input type="button" value="Clear" /></a>
</form>
</div>
<body>
<p>{{ filter.qs.count }} circuits returned</p>
<div>
<table style="width:100%">
<thead class="tabledata">
<tr>
<th>View</th>
<th>CircuitID</th>
<th>Bandwidth</th>
<th>Region</th>
<th>Carrier</th>
<th>Status</th>
<th>Segmentname</th>
<th>MRC</th>
</tr>
</thead>
<tbody>
{% for circuit in object_list %}
<tr class="tablerow">
<td class="actions">
<a href="/viewLit/{{ circuit.circuitid }}" target="_blank" class="view-item" title="View">View</a>
</td>
<td>{{ circuit.circuitid }}</td>
<td>{{ circuit.bandwidth }}</td>
<td>{{ circuit.region }}</td>
<td>{{ circuit.carrier }}</td>
<td>{{ circuit.status }}</td>
<td>{{ circuit.segmentname }}</td>
<td>{{ circuit.mrcnew }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</div>
{% endblock %}
</html>
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e29d994-473f-496a-b2c3-a7e93b47bb3a%40googlegroups.com.
No comments:
Post a Comment