I have a homepage which currently runs a forloop to show all users.
{% for user in users %}
On that same page, there is a form to filter by radius (km) to see users close to you.
Currently, after you submit a radius to filter the users, nothing is returned because there is no
{% for user in location %} forloop yet.
I know I need some
{% if xxx %} {% else %} {% endif %}
statements but I'm unsure how to write them.
Essentially, if no filter is selected / radius is submitted I want to return all users (like it does right now). But if a radius is submitted or a filter is applied, I want the page to return the results of the queryset / multiple querysets.
Many thanks and any suggestions on cleaning up my code are always appreciated as I am a beginner.
views.py
class ConnectView(View):
template_name = 'connect/home.html'
def get(self, request, *args, **kwargs):
context = {
'users': User.objects.exclude(username=request.user),
'friends': Friend.objects.filter(current_user=request.user),
}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
location = Location(latitude=request.POST['latitude'], longitude=request.POST['longitude'], user = request.user)
location.save()
return JsonResponse({'message': 'success'})
def location(request):
if request.POST:
radius_km = request.POST.get('radius', 0)
queryset = User.objects.annotate(
radius_sqr=pow(models.F('loc__latitude') -
request.user.loc.latitude, 2) + pow(models.F('loc__longitude') -
request.user.loc.longitude, 2)
).filter(
radius_sqr__lte=pow(int(radius_km) / 9, 2)
).exclude(username=request.user)
context = dict(location=queryset)
return render(request, 'connect/home.html', context)
urls.py
urlpatterns = [
path('', connect_views.ConnectView.as_view(), name='connect_home'),
path('location', connect_views.location, name='location'),
connect.html
<h1>Connect with people near you.</h1>
<!-- GET window.location IP Address / lat lon coordinates -->
<p id="demo"></p>
<button onclick="getLocation()" class="btn btn-warning" id="confirm">1. Fetch Location</button>
<button type="submit" id="btn_submit" class="btn btn-success" disabled>2. Submit Location </button>
<!-- enter radius to filter by location -->
<form action="location" method="POST">
{% csrf_token %}
<input type="number" name="radius">
<input type="submit" value="filter by kilometers">
</form>
{% for user in users %}
<h4>{{ user.first_name }} {{ user.last_name }}
{{ user.profile.age }} / {{ user.profile.city }}
{{ user.profile.bio }}</h4>
{% endfor %}
for user in usersto return all users if no radius has been entered andfor user in locationif a radius has been submitted. I'm struggling with writing theif thenstatements for that to occur.locationview. How is that called and how does it relate to ConnectView?locationview is the function that takes in the radius in order to generate out a queryset of users.locationview is called via a submit form on theConnectViewpage