Answer the question
In order to leave comments, you need to log in
How to show data in django template filtered by user criteria?
I learn django (and the web in general) by trial and error on a home project.
There is a simple model:
class CrArchive(models.Model):
cr_id = models.IntegerField(primary_key=True)
type = models.ForeignKey(CrType, db_index=True)
cr_description = models.TextField(blank=True)
latitude = models.FloatField(blank=True)
longitude = models.FloatField(blank=True)
class Meta:
managed = False
db_table = 'cr_archive'
For reference, I wrote a view for her:def index(request):
latest_crime_list = CrArchive.objects.order_by('-cr_id')[:5]
context = {'latest_crime_list': latest_crime_list, }
return render(request, 'archive/archive.html', context)
<div class="row">
{% if latest_crime_list %}
{% for crime in latest_crime_list %}
<div class="col-md-12">
<div class="bs-callout bs-callout-warning">
<h4>{{ crime.latitude }} | {{ crime.longitude }} | {{ crime.type }}</h4>
<p>{{ crime.cr_description }}</p>
</div>
</div>
{% endfor %}
{% endif %}
</div>
Answer the question
In order to leave comments, you need to log in
Familiarize yourself with forms
https://docs.djangoproject.com/en/dev/topics/forms/
Get data, validate, then you can use this data as you like.
For example:
latest_crime_list = CrArchive.objects.filter(latitude=latitude).order_by('-cr_id')[:5]
As an option, if the coordinates are saved and you can choose from the existing ones instead of forcing the user to enter, then extract the parameter from the address:
url(r'^/places/latitude/(.+)', places_views.show_latitude),
and in views use this option.
Examples and docks shaft, write in detail vlom. If you want, put the project on bitbucket or github - it will be faster to show the code than to explain :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question