Django template rendering not giving expected results - python

I am rendering data from my views into my template, as follows:
<tbody>
{% for item in lyrics %}
<tr class='lyrics-table'>
<td>{{item}}</td>
<td>
{% if item in user_flash %}
<p>{{flash}}</p>
{% else %}
<p>xxx</p>
{% endif %}
</td>
{{item}} works as expected, but {{flash}} only gives the same value for every row, instead of the relevant value.
My views are as follows:
class SongVocab(LoginRequiredMixin, generic.DetailView):
model= models.Song
template_name = 'videos/song_vocab.html'
context_object_name = 'song'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
from pymystem3 import Mystem
m = Mystem()
user_flash = Flashcard.objects.filter(owner=self.request.user).values_list('question', flat=True)
lyrics_list = models.Song.objects.get().lyrics_as_list()
user_flash_ = [item.replace('\n', ' ') for item in m.lemmatize(" ".join(user_flash))]
user_flash_clean = [w for w in user_flash_ if w.strip()] ##removes empty strings
lyrics_list_ = [item.replace('\n', ' ') for item in m.lemmatize(" ".join(lyrics_list))]
lyrics_list_clean = [w for w in lyrics_list_ if len(w.strip())]
user_word = list(set(user_flash_clean) & set(lyrics_list_clean))
import icu # PyICU
def sorted_strings(strings, locale=None):
if locale is None:
return sorted(strings)
collator = icu.Collator.createInstance(icu.Locale(locale))
return sorted(strings, key=collator.getSortKey)
context['percent_known'] = ((len(user_word))/(len(set(lyrics_list_clean))))*100
context['lyrics'] = sorted_strings(set(lyrics_list_clean),"ru_RU.UTF8")
context['user_flash'] = user_flash_clean
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash'] = flash.answer
return context
I thought that using the for loop would let me get flash.answer for all words in user_word. In the example I'm testing, there should be two words, but I get just one. What am I doing wrong?
Models.py:
class Flashcard(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
deck = models.ForeignKey(Deck, on_delete=models.CASCADE)
question = models.TextField()
answer = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
last_shown_at = models.DateTimeField(auto_now_add=True)
next_due_date = models.DateTimeField(default=timezone.now)
difficulty = models.FloatField(default=2.5)
consec_correct_answers = models.IntegerField(default=0)
objects = FlashcardManager()
def __str__(self):
return self.question
def number_of_questions(self):
return self.question.count(deck=deck.id)

In your code
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash'] = flash.answer
context['flash'] will hold the last flash.answer, because the last line is outside the for-loop (it has one indentation level less than the second line).
Did you perhaps mean something like this?
context['flash_list'] = []
for word in user_word:
flash = Flashcard.objects.get(owner=self.request.user, question=word)
context['flash_list'].append(flash.answer)

Related

Django ordering search results from get_queryset(self)

I am using django 2.2.10. I have a searchbar which returns search results to the showpage, using get_queryset(self) in searchresultsview(listview) class. I have set paginate_by=10. In the front-end I made links to order the table: `
<th>Title original </th>
At the start of the get_queryset(self) function I have the following code:
order_by = self.request.GET.get('order_by')
direction = self.request.GET.get('direction')
if order_by is not None and order_by != "" and direction is not None and direction != "":
ordering = Lower(order_by)
if direction == 'desc':
ordering = '-{}'.format(ordering)
publications = Publication.objects.filter(is_deleted=False).order_by(ordering)
'''
paginator = Paginator(publications, 10)
page = self.request.GET.get('page')
try:
all_publications = paginator.page(page)
except PageNotAnInteger:
all_publications = paginator.page(1)
except EmptyPage:
all_publications = paginator.page(paginator.num_pages)
'''
return publications
The main problem is that the publications variable contain all publications, I want to restrict it to the publications from the previous get_queryset(self) call. Also the showpage is paginated (paginate_by = 10 in template).The ordering for descending does not work I get: invalid order_by arguments: ['-Lower(F(title_original))'] . If I order ascending it logically does not keep my search results.
I tried finding a stackoverflow solution but the explanation is minimal. Any help would be appreciated.
It might be easier to use django-tables, if so I am open for suggestions.
view codes:
class SearchResultsView(ListView):
'''
ListView of the initial search page.
The function get_queryset works for the search bar and the search form home page.
The search bar typically uses q for query otherwise a id for list search.
Use a countries_dict to convert for example Netherlands to NL so that search succeeds.
If a normal field is searched use __icontains if a list element is searched use: __in.
'''
model = Publication
template_name = 'publications/show.html'
context_object_name = 'publications'
publications = Publication.objects.filter(is_deleted=False)
#paginator = Paginator(publications, 10)
#paginator = Paginator(publications, 25)
paginate_by = 10
def get_ordering(self):
order_by = self.request.GET.get('order_by')
direction = self.request.GET.get('direction')
if order_by is not None and order_by != "" and direction is not None and direction != "":
ordering = Lower(order_by)
if direction == 'desc':
ordering = '-{}'.format(ordering)
return ordering
def get_queryset(self):
#form = PublicationForm(self.request.GET)
authors = self.request.GET.getlist('author')
translators = self.request.GET.getlist('translator')
authors = Author.objects.filter(pk__in=authors).all()
translators = Translator.objects.filter(pk__in=translators).all()
form_of_publications = self.request.GET.getlist('form_of_publication')
form_of_publications = FormOfPublication.objects.filter(pk__in=form_of_publications).all()
languages = self.request.GET.getlist('language')
languages = Language.objects.filter(pk__in=languages).all()
affiliated_churches = self.request.GET.getlist('affiliated_church')
affiliated_churches = Church.objects.filter(pk__in=affiliated_churches).all()
content_genres = self.request.GET.getlist('content_genre')
content_genres = Genre.objects.filter(pk__in=content_genres).all()
connected_to_special_occasions = self.request.GET.getlist('connected_to_special_occasion')
connected_to_special_occasions = SpecialOccasion.objects.filter(pk__in=connected_to_special_occasions).all()
currently_owned_by = self.request.GET.getlist('currently_owned_by')
currently_owned_by = Owner.objects.filter(pk__in=currently_owned_by).all()
copyrights = self.request.GET.get('copyrights')
is_a_translation = self.request.GET.get('is_a_translation')
publications = Publication.objects.filter(is_deleted=False)
uploadedfiles = self.request.GET.getlist('uploadedfiles')
uploadedfiles = UploadedFile.objects.filter(pk__in=uploadedfiles).all()
keywords = self.request.GET.getlist('keywords')
keywords = Keyword.objects.filter(pk__in=keywords).all()
translated_from = self.request.GET.getlist('translated_From')
translated_from = Language.objects.filter(pk__in=translated_from).all()
city = self.request.GET.getlist('publication_city')
country = self.request.GET.getlist('publication_country')
collection_country = self.request.GET.getlist('collection_country')
if list(collection_country) != ['']:
collection_country = Country.objects.filter(pk__in=city).all()
if list(country) != ['']:
country = Country.objects.filter(pk__in=city).all()
print('....', city)
if list(city) != ['']:
city = City.objects.filter(pk__in=city).all()
print(publications)
exclude = ['csrfmiddlewaretoken','search']
in_variables = [('author', authors), ('translator', translators), ('form_of_publication', form_of_publications), ('language',languages), ('affiliated_church', affiliated_churches) \
, ('content_genre', content_genres), ('connected_to_special_occasion', connected_to_special_occasions), ('currently_owned_by', currently_owned_by),\
('uploadedfiles', uploadedfiles), ('publication_country', country), ('publication_city', city), ('collection_country', collection_country), ('keywords', keywords), ('translated_from',translated_from)]
special_case = ['copyrights', 'page', 'is_a_translation']
if ('q' in self.request.GET) and self.request.GET['q'].strip():
query_string = self.request.GET['q']
if query_string.lower() in countries_dict.keys():
query_string = countries_dict[query_string.lower()]
search_fields = ['title_original', 'title_subtitle_transcription', 'title_subtitle_European', 'title_translation', 'author__name', 'author__name_original_language', 'author__extra_info', \
'form_of_publication__name', 'editor', 'printed_by', 'published_by', 'publication_date', 'publication_country__name', 'publication_city__name', 'publishing_organisation', 'translator__name', 'translator__name_original_language', 'translator__extra_info', \
'language__name', 'language__direction', 'affiliated_church__name', 'extra_info', 'content_genre__name', 'connected_to_special_occasion__name', 'donor', 'content_description', 'description_of_illustration', \
'nr_of_pages', 'collection_date', 'collection_country__name', 'collection_venue_and_city', 'contact_telephone_number', 'contact_email', 'contact_website', \
'currently_owned_by__name', 'uploadedfiles__description', 'uploadedfiles__uploaded_at', 'general_comments', 'team_comments', 'other_comments', 'keywords__name', 'is_a_translation', 'ISBN_number', 'translated_from__name', 'translated_from__direction']
arabic_query = translator.translate(query_string, dest='ar').text
query_string = to_searchable(query_string)
#arabic_query = to_searchable(arabic_query)
entry_query = get_query(query_string, search_fields)
arabic_query = get_query(arabic_query, search_fields)
print('&&&&&&', query_string)
#publications = publications.filter(entry_query)
publications = publications.filter(Q(entry_query) | Q(arabic_query))
print(publications)
publications = publications.distinct()
return publications
for field_name in self.request.GET:
get_value = self.request.GET.get(field_name)
if get_value != "" and not field_name in exclude and not field_name in [i[0] for i in in_variables] and\
not field_name in special_case:
print('******', field_name)
arabic_query = translator.translate(get_value, dest='ar').text
get_value = to_searchable(get_value)
get_value = get_query(get_value, [field_name])
arabic_query = get_query(arabic_query, [field_name])
print('444444444', get_value)
publications = publications.filter(Q(get_value) | Q(arabic_query))
print('55555555555', publications)
#publications = publications.filter(Q(**{field_name+'__regex':get_value}) | Q(**{field_name+'__icontains':arabic_query}) )
for field_name, list_object in in_variables:
print('****', list_object)
if list_object:
print('------', field_name)
if list(list_object) != ['']:
publications = publications.filter(**{field_name+'__in': list_object})
if str(copyrights) != "unknown" and str(copyrights) != "None":
val = False
if str(copyrights) == "yes":
val = True
print('11111', str(copyrights))
publications = publications.filter(copyrights=val)
print('666666', publications)
if str(is_a_translation) != "unknown" and str(is_a_translation) != "None":
val = False
if str(is_a_translation) == "yes":
val = True
print('11111', str(is_a_translation))
publications = publications.filter(is_a_translation=val)
publications = publications.distinct()
return publications
You can use get_ordering method
def get_ordering(self):
ordering = self.request.GET.get('ordering', ''#default order param)
return ordering
I solved this. Basically if you have a get_queryset method you need to call get_ordering from there. Also for pagination you need to have the sorting variables in context so that if you go to page 2 for example, the ordering is maintained. Below is the solution code:
ordering = self.get_ordering()
if ordering is not None and ordering != "":
publications = publications.order_by(ordering)
return publications
def get_context_data(self, **kwargs):
context = super(SearchResultsView, self).get_context_data(**kwargs)
order_by = self.request.GET.get('order_by')
if order_by is not None and order_by != "":
context['order_by'] = order_by
context['direction'] = self.request.GET.get('direction')
else:
context['order_by'] = ''
context['direction'] = ''
q = self.request.GET.get('q')
if q is not None and q != "":
context['q'] = q
else:
context['q'] = ''
return context
And the html code:
{% extends "base.html" %}
{% block content %}
<table class="table table-striped table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>Title original </th>
<th>Title subtitle transcription </th>
<th>Title translation </th></th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<br />
{% for publication in publications %}
<tr id="publications">
<td style="text-align: start;unicode-bidi: plaintext;">{{ publication.title_original }}</td>
<td>{{ publication.title_subtitle_transcription}}</td>
<td>{{ publication.title_translation }}</td>
<td>
View
<span class="glyphicon glyphicon-pencil" >Edit</span>
<a class="confirm-delete-pub" href="/publication/{{ publication.id }}/delete">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
{% endblock %}

Is there an effcient way to perform search query in django?

I'm creating a blog in which i want to perform a search query based on ones rating (1-5). Here my search would be like query:"smart phone tech updates", rating:"3". Result should be list of post that contains query word(at least one word) which has rating 3, in a sorted way on val(for each query word, if found in title val+=1 if found in content val+=0.4).
My models.py file has the following :
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.CharField(max_length=500)
rating = models.IntegerField(default=1)
enter code here
date = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
My view.py file has the following:
def search(request):
contents = Post.objects.all()
if request.method == 'GET':
query = request.GET['query']
rating = request.GET['rating']
# search function
# contents = InSearch.search_fun(contents,query,rating)
vector = SearchVector('title', weight='A') + SearchVector('content', weight='B')
qry = SearchQuery(str(query))
contents = Post.objects.annotate(rank=SearchRank(vector, qry)).order_by('-rank')
#print("---->\n\t"+query+ str(contents))
context = {
'contents': contents
}
else:
context = {
'contents': Post.objects.all()
}
return render(request, 'feed/home.html', context)
My urls.py is:
urlpatterns = [
#...
path('feed/search-result/', views.search, name='feed_search'),
]
I'm getting this error
django.db.utils.OperationalError: no such function: plainto_tsquery
You can try like this in your views for searching.
from django.db.models import Q
def search(request):
q = request.GET.get('q')
if q:
search_results = Post.objects.filter(Q(title__icontains=q)|Q(rating=q))
# if you want the exact then do Post.objects.filter(Q(title__iexact=q) & Q(rating=q))
return render(request, 'feed/home.html','search_results':search_results)
else:
messages.info(request,'no results found for {}',format(q))
If you want to sort search query result by number of matches then you can try like this:
search_results = Post.objects.filter(Q(title__icontains=q)|Q(rating=q)).annotate(title_counts=Count('title')).order_by('-title_counts')
And in your template give name='q' in the search form.
<form action="{% url 'your search action' %}">
<input type="text" name="q">
<input type='submit' value='Search'>
</form>

mysql returning 1 row per id/name

I want to join 3 tables and get the results of them without any duplicates
SELECT * FROM `database`.project
INNER JOIN post on project.id = post.project_id
INNER JOIN media on media.post_id = post.id
;
Current output
I was wondering if the output could be something like
floaty
Headphone
fasion + technolgie
I tried using the distinct function. but then it only returns the names's i would like to return the joined tables, because i still want to use that data.
models.py
I am using the Project.with_media() all
"""models."""
from app import db
from peewee import *
import datetime
class Project(Model):
"""Projects."""
name = CharField(unique=True)
content = CharField()
created_date = DateTimeField(default=datetime.datetime.today())
class Meta(object):
"""Select database."""
database = db
def get_project_media(self):
"""Grab image from get_posts."""
post = Post.select().where(Post.project_id == self).get()
return Media.select().where(Media.post_id == post).get().media
def check_media(self):
"""Check if project has media."""
try:
post = Post.select().where(Post.project_id == self).get()
Media.select().where(Media.post_id == post.id).get()
print('True')
return True
except DoesNotExist:
print('False')
return False
This is my calling so i can display it on jinja engine
def with_media():
"""Grab image from get_posts."""
return (Project.select(Project, Post, Media)
.join(Post)
.join(Media)
.where(Post.id == Media.post_id
and
Project.id == Post.project_id))
def posts(self):
"""Return all posts that are accosicated with this project."""
return Post.select().where(Post.project_id == self)
def media_post(self):
"""Return all posts that are accosicated with this project."""
post = Post.select().where(Post.project_id == self)
return post.get_media()
# return Media.select().where(Media.post_id == post).get()
class Post(Model):
"""Model for posts."""
project = ForeignKeyField(Project, backref='Post', null=True, default=None)
name = CharField()
content = TextField()
"Media Model"
"Category Model"
"Project Model"
created_date = DateTimeField(default=datetime.datetime.today())
class Meta(object):
"""Select database."""
database = db
def get_category(self):
"""Grab all the posts from project."""
return (Category.select()
.where(Category.post_id == self))
def get_media(self):
"""Grab all media from this post."""
return (Media.select()
.where(Media.post_id == self))
def standalone():
"""Return a model of all posts not bound to a project."""
return (Post.select()
.where(Post.project.is_null())
.order_by(Post.created_date.desc()))
def date():
"""Return dates order_by."""
return(Post.select()
.order_by(Post.created_date.desc()))
class Media(Model):
"""Media for post."""
post = ForeignKeyField(Post, backref='Media')
media = CharField()
class Meta(object):
"""Select database."""
database = db
class Category(Model):
"""model for all avaible category's."""
post = ForeignKeyField(Post, backref='Category')
name = CharField()
class Meta(object):
"""Select database."""
database = db
def get_name():
"""Get all category's without overlaping."""
categorys = Category.select()
categoryList = []
for category in categorys:
categoryName = category.name.title()
if categoryName not in categoryList:
categoryList.append(categoryName)
return categoryList
def initialize():
"""Create tables."""
db.connect()
db.create_tables([Category, Project, Post, Media], safe=True)
db.close()
main.py
I want to call the projects with media() function so i can use the database items to call images and display content
<ul class='projects'>
{% for project in projects.with_media() %}
{% if loop.index <= 3 %}
<li class='project_w'>
<img src="{{project.media_post()}}" alt="">
<a href="{{url_for('project', id=project.id)}}">
<h2>{{project.name}}</h2>
</a>
</li>
{% else %}
<li class='project_h'>
<img src="{{project.post.media.media}}" alt="">
<a href="{{url_for('project', id=project.id)}}">
<h2>{{project.name}}</h2>
</a>
</li>
{% endif %}
{% endfor %}
</ul>
The problem:
.where(Post.id == Media.post_id
and
Project.id == Post.project_id))
Instead of "and" you must use "&". Please see http://docs.peewee-orm.com/en/latest/peewee/query_operators.html

After importing variable from view.py into. No dropdown appearing in forms

I am using Django forms in in forms I need a variable from views.py named 'layer_id'. So I get it like
self.form = labelModelForm(layer_id=self.layer.id)
and used in forms.py like
class labelModelForm(forms.ModelForm):
model = Labels_tool_
def __init__(self, *args , **kwargs):
layer_id = kwargs['layer_id']
apn= forms.ModelChoiceField(queryset=Field.objects.filter(layer=layer_id).values_list('name', flat=True) ,empty_label="(Choose field)")
So now when I run program no dropdown list appear. Where I am wrong ? As layer_id I am getting is correct in form in view.py
My View.py file is
def ImportLabelView(self):
urlItems =self.request.path.split('/')
i = urlItems.index('layers')
self.form = labelModelForm(layer_id=self.layer.id)
if self.request.method == 'POST':
self.layer = Layer.objects.filter(id=urlItems[i + 1],map=self.map.id).first()
layerid= self.layer.id
labmapid=self.map.id
OwnerName = self.request.POST.get('owner_name')
MailingAddrOne = self.request.POST.get('mailing_addr_One')
mailingaddrTwo = self.request.POST.get('mailing_addr_Two')
ApN = self.request.POST.get('apn')
situsaddrTwo = self.request.POST.get('situs_addr_Two')
situsaddrOne = self.request.POST.get('situs_addr_One')
if Labels_tool_.objects.filter(map_id_labels=labmapid ,layer_id_labels=layerid).exists():
Labels_tool_.apn = Labels_tool_.objects.filter(map_id_labels = labmapid , layer_id_labels = layerid).update(apn=ApN)
Labels_tool_.owner_name = Labels_tool_.objects.filter(map_id_labels=labmapid, layer_id_labels=layerid).update(owner_name=OwnerName)
Labels_tool_.mailing_addr_One = Labels_tool_.objects.filter(map_id_labels=labmapid,
layer_id_labels=layerid).update(mailing_addr_One=MailingAddrOne)
Labels_tool_.mailing_addr_Two = Labels_tool_.objects.filter(map_id_labels=labmapid,
layer_id_labels=layerid).update(mailing_addr_Two=mailingaddrTwo)
Labels_tool_.situs_addr_One = Labels_tool_.objects.filter(map_id_labels=labmapid, layer_id_labels=layerid).update(situs_addr_One=situsaddrOne)
Labels_tool_.situs_addr_Two = Labels_tool_.objects.filter(map_id_labels=labmapid, layer_id_labels=layerid).update(situs_addr_Two=situsaddrTwo)
else:
labels_tool = Labels_tool_()
labels_tool.apn = self.request.POST.get('apn')
labels_tool.owner_name = self.request.POST.get('owner_name')
labels_tool.mailing_addr_One= self.request.POST.get('mailing_addr_One')
labels_tool.mailing_addr_Two= self.request.POST.get('mailing_addr_Two')
labels_tool.situs_addr_One = self.request.POST.get('situs_addr_One')
labels_tool.situs_addr_Two = self.request.POST.get('situs_addr_Two')
labels_tool.map_id_labels = self.map.id
labels_tool.layer_id_labels = self.layer.id
labels_tool.save()
# self.form = self.layer.form(self.request.POST)
return self.redirect('mapport.maps.layers.importlabel', self.map.id, self.layer.id)
return self.render('mapport/maps/layers/Labels_detail.html')
And template file is
<form action="" method="post" id="label_form">{% csrf_token %}
<div id="field1"><p id="apn_text">APN: </p> {{ form.apn}}</div>
Models are like
class pdftabel_tool_(models.Model):
apn = models.CharField(null=True, blank=False, max_length=255)
Now where I am making mistake that value from view.py is correct but not dropdown showing ?
You need to set self.fields, not self.initial.
You should define the field in the normal way at class level, then modify the queryset in the init method.
class labelModelForm(forms.ModelForm):
apn = forms.ModelChoiceField(queryset=Field.objects.none())
def __init__(self, *args, **kwargs):
layer_id = kwargs.pop('layer_id', None)
super(labelModelForm, self).__init__(*args, **kwargs)
self.fields['apn'].queryset = Field.objects.filter(layer=layer_id)

Initial (default) value on Django ChoiceFilter select box

I am trying to set initial, default value on select box created by DjangoFilter ChoiceFilter (more info here). Code seems to be pretty simple:
class MyFilter(FilterSet):
CHOICES = (
('', 'Any'),
('closed', 'Closed'),
('new', 'New'),
)
my_select_box = ChoiceFilter(choices=CHOICES, initial='new')
class Meta:
model = MyModel
fields = ('my_select_box')
def __init__(self, *args, **kwargs):
super(MyFilter, self).__init__(*args, **kwargs)
self.form.helper = FormHelper()
self.form.helper.form_method = 'get'
self.form.helper.add_input(Submit('submit', 'Search'))
self.form.helper.form_class = 'form-inline'
self.form.helper.field_class = 'm-r m-b-sm'
self.form.helper.label_class = 'm-r'
On views.py:
class MyView(BaseListView):
content_template = "template.html"
model = MyModel
filter_class = MyFilter
table_class = MyTable
class BaseListView(BaseView, MultipleObjectMixin, SingleTableMixin):
table_class = None
filter_class = None
queryset = None
list = None
dictionary = {}
def get_context_data(self, **kwargs):
if self.list is not None:
self.object_list = self.list
else:
if self.queryset is None:
self.queryset = self.get_queryset()
self.object_list = self.queryset
if self.filter_class is not None:
filter = self.filter_class(self.request.GET, queryset=self.object_list)
table = self.table_class(filter.qs)
# filter.form.fields['my_select_box'].default = 'new'
# filter.form.initial['my_select_box'] = 'new'
ids = json.dumps(list(filter.qs.values_list('id', flat=True)))
else:
filter = ""
table = self.table_class(self.object_list)
ids = [obj['id'] for obj in self.object_list] if self.object_list is list else None
RequestConfig(self.request, paginate={"per_page": 10}).configure(table)
kwargs['content'] = render_to_string(self.content_template, context_instance=RequestContext(self.request),
dictionary=dict(
{'filter': filter, 'object_list': self.object_list, 'ids_list': ids,
'table': table}.items() + self.dictionary.items()))
context = super(BaseListView, self).get_context_data(**kwargs)
return context
On template.html:
<div class="panel-body">
<section class="panel panel-default">
<header class="panel-heading font-bold">
Filter
</header>
<div class="panel-body">
{% crispy filter.form %}
</div>
</section>
{% render_table table %}
</div>
On template side I can see with debugger that form.my_select_box.initial is set to be 'new'. But well, its not working (instead of 'new' I was trying also integers). If I change CHOICES to be like this:
CHOICES = (
('any', 'Any'),
('closed', 'Closed'),
('', 'New'),
)
It seems to work - but I don't have value from "New" selection, which makes things rough later - I can of course swap my '' value to become 'new', and 'any' to become '' on the backend in views.py by editing request.GET (not pretty solution though), but after page refresh if I used New, I see results for New, but selections displays Any (and opposite).
I believe solution can be achieved on server side (I don't want JS)?

Categories

Resources