structure of model is i am using hvad for multilingual
class Course(TranslatableModel):
translations = TranslatedFields(
domain = models.ForeignKey('domain.Domain'),
#track = models.ForeignKey('track.Track'),
track = models.ForeignKey('track.Track', blank=True, null=True),
course_nm = models.CharField(max_length=100, verbose_name =_('Course Name')),
nature_of_training = models.TextField(verbose_name = _('Nature of Training')),
duration = models.PositiveIntegerField(verbose_name =_('Duration')),
sem = models.PositiveIntegerField(verbose_name =_('Semester')),
degree_level = models.CharField(max_length=100, verbose_name =_('Degree Level')),
credit = models.PositiveIntegerField(verbose_name =_('Credits')),
locations = models.CharField(max_length=100, verbose_name =_('Locations')),
accessible = models.CharField(max_length=100, verbose_name =_('Accessible')),
des = models.TextField(verbose_name = _('Description')),
admission_details = models.TextField(verbose_name = _('Admission Details')),
further_study_details = models.TextField(verbose_name = _('Further Study Details')),
seats = models.PositiveIntegerField(verbose_name =_('Seats')),
title = models.CharField(max_length=512, verbose_name=_('Title')),
slug = models.SlugField(max_length=512, verbose_name=_('Slug')),
created_date = models.DateTimeField(auto_now_add=True, blank=True, null=True),
updated_date = models.DateTimeField(auto_now=True, blank=True, null=True),
created_by = models.ForeignKey(User, blank=True, null=True, editable=False),
)
pass object in context from view to template for sorting i apply this,
in views.py i simply pass objects in context
def get_context_data(self, **kwargs):
context = super(DegreeDetailView, self).get_context_data(**kwargs)
context['courses'] = Course.objects.all()
return context
in template
{% for c in courses|dictsort:"track" %}
{% if c.domain|stringformat:"s" == d.domain_nm %}
{% if c.track != None %}
<h3>{{ c.track }}</h3>
{% endif %}
{{ c.course_nm }}
{% endif %}
{% endfor %}
But After refresh page each time results are change,
so what i should do for sorting in django template,or any alternative is possible for sorting in django template I try a lot but not find solution,
Thanks in Advance!!
I think, that the reason, why you get different results, is because
track
can benull
:So, courses with
track
field set tonull
will not have exact order, and therefore they will be rendered in random positions. So, either you need alltrack
fields to be set to some value (and probably removeblank=True, null=True
+ apply migrations), either sort by another field.Also, sorting models using
dictsort
in templates is not the best way to do. Better do it at database level, providingorder_by
to you queryset: